匿名函数和内置函数的使用

1 potfofolio=[
2     {'name':'IBM','shares':100,'price':91.1},
3     {'name':'APPLE','shares':50,'price':543.22},
4     {'name':'FB','shares':300,'price':21.09},
5     {'name':'HPQ','shares':35,'price':31.75},
6     {'name':'YHOO','shares':45,'price':16.35},
7     {'name':'INTEL','shares':75,'price':115.65},
8 ]

显示输出总价格:shares*price

实现代码:

m1=map(lambda x:{x['name']:x['shares']*x['price']},potfofolio)   #返回值是一个生成器
for i in m1:
    print(i)

显示价格大于100的记录:

实现代码:

1 f1=filter(lambda x:int(x['price'])>100,potfofolio)      #返回满足条件的记录 还是原来的字典样式
2 for i in f1:
3     print(list(i.values()))

猜你喜欢

转载自www.cnblogs.com/wen-kang/p/9341367.html