python Day_16作业
用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb
name=['alex','wupeiqi','yuanhao','nezha'] def csb(n): n = n+'_sb' return n print(list(map(csb,name)))
用filter函数处理数字列表,将列表中所有的偶数筛选出来
num = [1,3,5,6,7,8] def d(n): if n % 2 == 0: return n print(list(filter(d,num)))
随意写一个20行以上的文件,运行程序,先将内容读到内存中,用列表存储。接收用户输入页码,每页5条,仅输出当页的内容
def pri(lines,n): page = [] if n > len(lines)//5+1: print('页面超范围') return for i in range(0,len(lines)//5+1): page.append(lines[i*5:i*5+5]) for i in page[n-1]: print(i) print('第%s页 共%s页' % (n,len(lines)//5+1)) with open('Day16','r',encoding='utf-8') as f: f.seek(0) lines = f.readlines() while True: put_page = input('请输入页数或退出').strip() if put_page.isdigit(): put_page = int(put_page) pri(lines,put_page) elif put_page == 'q':break else:print('请输入正确数字')
如下,每个小字典的name对应股票名字,shares对应多少股,price对应股票的价格
1.计算购买每支股票的总价
def my_sum(d): per_sum = d['shares']*d['price'] return d['name'],per_sum print(list(map(my_sum,portfolio)))
2.用filter过滤出,单价大于100的股票有哪些
def pricemax(d): if d['price'] > 100: return d['name'] print(list(filter(pricemax,portfolio)))