#author:徐征 #=====================作业一 #用map来处理字符串列表啊,把列表中所有人都变成sb,比方lin_sb name=['lin','hai','feng'] m=map(lambda name:name+"_NB",name) print(list(m)) '''['lin_NB', 'hai_NB', 'feng_NB']''' #用map来处理下述l,然后用list得到一个新的列表,列表中每个人的名字都是sb结尾 l=[{'name':'egon'},{'name':'d'}] print(list(map(lambda name:name["name"]+"_NB", l))) '''['egon_NB', 'd_NB']''' #=====================作业二 #用filter来处理,得到股票价格大于20的股票名字 shares={ 'IBM':36.6, 'Lenovo':23.2, 'oldboy':21.2, 'ocean':10.2, } f=filter(lambda price:shares[price]>20,shares) for i in f: print(i) ''' IBM Lenovo oldboy ''' # =====================作业三 #如下,每个小字典的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:map来得出一个包含数字的迭代器,数字指的是:购买每支股票的总价格 z=map(lambda total_prices:{total_prices["name"]:total_prices["shares"]*total_prices["price"]},portfolio) for i in z: print(i) ''' {'IBM': 9110.0} {'AAPL': 27161.0} {'FB': 4218.0} {'HPQ': 1111.25} {'YHOO': 735.7500000000001} {'ACME': 8673.75} ''' #2:基于1的结果,用reduce来计算,购买这些股票总共花了多少钱 from functools import reduce z=map(lambda total_price:total_price["shares"]*total_price["price"],portfolio) print(reduce(lambda x,y:x+y,z)) '''51009.75''' #3:用filter过滤出,单价大于100的股票有哪些 f=filter(lambda price:price["price"] > 100,portfolio) for i in f: print(i) ''' {'name': 'AAPL', 'shares': 50, 'price': 543.22} {'name': 'ACME', 'shares': 75, 'price': 115.65} '''