如下,每个小字典的name对应股票名字,shares对应多少股,price对应股票的价格
portfolio = [
{‘name’: ‘IBM’, ‘shares’: 100, ‘price’: 91.1},
{‘name’: ‘AAPL’, ‘shares’: 50, ‘price’: 543.22},
{‘name’: ‘FB’, ‘shares’: 200, ‘price’: 21.09},
{‘name’: ‘HPQ’, ‘shares’: 35, ‘price’: 31.75},
{‘name’: ‘YHOO’, ‘shares’: 45, ‘price’: 16.35},
{‘name’: ‘ACME’, ‘shares’: 75, ‘price’: 115.65}
]
1.通过哪个内置函数可以计算购买每支股票的总价
print(list(map(lambda dic: {dic['name']: round(dic['shares'] * dic['price'])}, portfolio)))

 


2.用filter过滤出,单价大于100的股票有哪些?
1  f= filter(lambda x:x['price'] > 100,portfolio)
2 print(list(f))

 

posted on 2019-07-05 17:07  嗯哈哈  阅读(1207)  评论(0编辑  收藏  举报