3.用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb
name=['alex','wupeiqi','yuanhao','nezha'] name1 =[] def func(s): return (s+"_sb") t = map(func,name) # t是迭代器 for i in t: print(i) name1.append(i) print(name1)
4.用filter函数处理数字列表,将列表中所有的偶数筛选出来
num = [1,3,5,6,7,8] def func(s): return s % 2 == 0 t = filter(func,num) #迭代器 for i in t: print(i)
-------------------------------
s = filter(lambda s:s % 2 == 0,num)
for i in s:
print(i)
# 5.随意写一个20行以上的文件 运行程序,先将内容读到内存中,用列表存储。 接收用户输入页码,每页5条,仅输出当页的内容
with open("1",encoding="utf-8") as f: l = f.readlines() # print(l) page,mod = divmod(len(l),5) #divmod(被除数,除数)-----商,余数 if mod: page +=1 #有余数则页数加一 while True: page_num = int(input("请输入页数:")) if page_num < 1: print("请输入正常页数") elif page_num>page: print("页数超出") else: str1 = l[(page_num - 1)*5:page_num *5] for i in str1: print(i.strip())