匿名函数作业
1、默写
1 L = [1,2,3,4] 2 def pow2(x): 3 return x*x 4 print(list(map(pow2,L)))
1 [1, 4, 9, 16]
1 L = [1,2,3,4] 2 def pow(x): 3 return x*x 4 print(list(map(pow,L)))
1 [1, 4, 9, 16]
1 def is_odd(x): 2 return x % 2 ==1 3 print(list(filter(is_odd,[1,4,6,7,9,12,17])))
1 [1, 7, 9, 17]
1 def is_odd(x): 2 return x % 2 == 1 3 print(list(filter(is_odd,[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19])))
1 [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
2、员工信息表的作业之前没做完的继续完善
3、用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb name = ['alex','wupeiqi','yuanhao','nezha']
1 name = ['alex','wupeiqi','yuanhao','nezha'] 2 print(name) 3 def add_f(x): 4 return (x + '_sb') 5 print(list(map(add_f,name))) #map返回的内容是迭代器,可迭代的是for循环里面时候才生成迭代器
1 ['alex', 'wupeiqi', 'yuanhao', 'nezha'] 2 ['alex_sb', 'wupeiqi_sb', 'yuanhao_sb', 'nezha_sb']
1 name = ['alex','wupeiqi','yuanhao','nezha'] 2 res = map(lambda x:x+'_sb',name) 3 print(list(res))
1 ['alex_sb', 'wupeiqi_sb', 'yuanhao_sb', 'nezha_sb']
4、用filter函数处理数字列表,将列表中所有的偶数筛选出来
num = [1,3,5,6,7,8]
1 num = [1,3,5,6,7,8] 2 def odd(x): 3 return x %2 == 0 4 print(list(filter(odd,num)))
1 [6, 8]
1 num = [1,3,5,6,7,8] 2 # res = filter(lambda x:x%2 == 0,num) 3 res = filter(lambda x:True if x%2 == 0 else False,num) # 4 print(list(res))
1 [6, 8]
1 filter(lambda x:x%2 == 0,num)里面其实开始是这样演变的: 2 第一步: filter(lambda ,num) 3 第二步:filter(lambda x:x%2 == 0 ,num) 4 5 filter(lambda x:True if x%2 == 0 else False,num) 6 上面这句表面lambda里面还可以包含简单的三元运算符,如果x是列表的话,那么我们可以把列表 7 里面的挨个元素进行判定,这样计算就更简单了 8 9 filter和map的返回值是一个迭代器, 10 map是对每个调用的参数都有结果,返回一个新列表 11 而filter只返回调用function以后成功的结果,返回一个新列表。 12 lambda后面只能有一句话的简单逻辑
5、随意写一个20行以上的文件
运行程序,先将内容读到内存中,用列表存储。
接收用户输入页码、每页5条,仅输出当页的内容
list = [] with open('log',mode='r',encoding='utf-8') as f: for i in f: list.append(i.strip()) print(list) while True: page = input("请输入页码:").strip() page_start = int(page)*5 print(list[page_start:page_start+5])
1 ['12333333333', '333e3', '彭dfdfdfdfd', 'dfdfdfdf', '大幅度发的', '让2任务分为如法网', '费大幅度', 'dfdfdfdfdfdfd', '打分的人', '发大幅度发的', 'dfdfdfdfdfd23223', '423若非法人', 'fdfdfe', '发大幅度发的发', 'fdfdfdffdfdff', '232323223', '22424232', '32fewfewfweff', 'dfdfff', '2323fewfw', '付费3', 'rfrfe', 'hbbd'] 2 请输入页码:2 3 ['dfdfdfdfdfd23223', '423若非法人', 'fdfdfe', '发大幅度发的发', 'fdfdfdffdfdff']
1 with open("log",encoding="utf-8") as f: 2 L = f.readlines() 3 print(L) 4 page_num = int(input("请输入页码:").strip()) #输入要打印的页码 5 page,mod = divmod(len(L),5) #计算有多少页,余数是多少 6 print(page,mod) 7 if mod: 8 page +=1 #这里计算是最终的页数,因为不满足5行也算一页 9 if page_num > page or page_num <= 0: #页数一般不能小于等于0,大于页数也有问题 10 print("输入有误") 11 if page_num > 0 and page_num < page: # 12 for i in range(5): 13 print(L[i+(page_num -1)*5].strip()) #打印 14 elif page_num == page and mod != 0:#如果用户输入的页码是最后一页且最后一页的行数小于5 15 for i in range(mod): 16 print(L[i + (page_num - 1)*5].strip())#只输出这页剩余的行
1 ['12333333333\n', '333e3\n', '彭dfdfdfdfd\n', 'dfdfdfdf\n', '大幅度发的\n', '让2任务分为如法网\n', '费大幅度\n', 'dfdfdfdfdfdfd\n', '打分的人\n', '发大幅度发的\n', 'dfdfdfdfdfd23223\n', '423若非法人\n', 'fdfdfe\n', '发大幅度发的发\n', 'fdfdfdffdfdff\n', '232323223\n', '22424232\n', '32fewfewfweff\n', 'dfdfff\n', '2323fewfw\n', '付费3\n', 'rfrfe\n', 'hbbd\n'] 2 请输入页码:2 3 4 3 4 让2任务分为如法网 5 费大幅度 6 dfdfdfdfdfdfd 7 打分的人 8 发大幅度发的
1 这道题考的是内置函数里面的divmod(a,b)这个知识点
6、如下,每个小字典的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}
]
计算购买每支股票的总价?
用filter过滤出单价大于100的股票有哪些?
1 portfolio = [ 2 {'name':'IBM','shares':100,'price':91.1}, 3 {'name':'AAPL','shares':50,'price':543.22}, 4 {'name':'FB','shares':200,'price':21.09}, 5 {'name':'HPQ','shares':35,'price':31.75}, 6 {'name':'YHOO','shares':45,'price':16.35}, 7 {'name':'ACME','shares':75,'price':115.65} 8 ] 9 total_price = portfolio[0]['shares'] * portfolio[0]['price'] 10 print(total_price) 11 def func(x): 12 return x['price'] > 100 13 for i in portfolio: 14 total = i['shares'] * i['price'] 15 # print("%s的购买总价是:" %i['name'], i['shares'] * i['price']) 16 print("%s的购买总价是:"%i['name'],total) 17 res = filter(func,portfolio) 18 for i in res: 19 print("%s的股票单价大于100"%i['name'])
1 9110.0 2 IBM的购买总价是: 9110.0 3 AAPL的购买总价是: 27161.0 4 FB的购买总价是: 4218.0 5 HPQ的购买总价是: 1111.25 6 YHOO的购买总价是: 735.7500000000001 7 ACME的购买总价是: 8673.75 8 AAPL的股票单价大于100 9 ACME的股票单价大于100
1 ret = map(lambda dic:{dic['name']:(dic['shares']*dic['price'])},portfolio) 2 print(list(ret)) 3 4 res = filter(lambda x:x['price'] > 100,portfolio) 5 print(list(res))
1 [{'IBM': 9110.0}, {'AAPL': 27161.0}, {'FB': 4218.0}, {'HPQ': 1111.25}, {'YHOO': 735.7500000000001}, {'ACME': 8673.75}] 2 [{'name': 'AAPL', 'shares': 50, 'price': 543.22}, {'name': 'ACME', 'shares': 75, 'price': 115.65}]
1 用了map、filter两个匿名函数,语句比较简洁