内置函数之sorted,filter,map

时间:2021-05-01 19:18:43
# 4,用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb
# name=['oldboy','alex','wusir']
# print(list(map(lambda i:i+"_sb",name)))
#5,用map来处理下述l,然后用list得到一个新的列表,列表中每个人的名字都是sb结尾
# l=[{'name':'alex'},{'name':'y'}]
# print(list(map(lambda dic:dic['name']+"_sb",l)))
#6,用filter来处理,得到股票价格大于20的股票名字
# shares={
# 'IBM':36.6,
# 'Lenovo':23.2,
# 'oldboy':21.2,
# 'ocean':10.2,
# }
# print(list(filter(lambda i:shares[i]>20,shares)))
#7.有下面字典,得到购买每只股票的总价格,并放在一个迭代器中。
# 结果:list一下[9110.0, 27161.0,......]
# 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}]
# print(list(map(lambda dic:dic['shares']*dic['price'],portfolio)))
#8.是上面的字典,用filter过滤出单价大于100的股票。
# 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}]
# print(list(filter(lambda dic:dic['price']<100,portfolio)))
#9有下列三种数据类型,
l1 = [1,2,3,4,5,6]
l2 = ['oldboy','alex','wusir','太白','日天']
tu = ('**','***','****','*******')
# 写代码,最终得到的是(每个元祖第一个元素>2,第三个*至少是4个。)
# [(3, 'wusir', '****'), (4, '太白', '*******')]这样的数据。
a=filter(lambda lst:lst[0]>2 and len(lst[2])>=4,list(i for i in zip(l1,l2,tu)))
print(list(a))
#有如下数据类型:
# l1 = [ {'sales_volumn': 0},
# {'sales_volumn': 108},
# {'sales_volumn': 337},
# {'sales_volumn': 475},
# {'sales_volumn': 396},
# {'sales_volumn': 172},
# {'sales_volumn': 9},
# {'sales_volumn': 58},
# {'sales_volumn': 272},
# {'sales_volumn': 456},
# {'sales_volumn': 440},
# {'sales_volumn': 239}]
# # 将l1按照列表中的每个字典的values大小进行排序,形成一个新的列表。
# print(sorted(l1,key=lambda dic:dic['sales_volumn']))