简述 filter map reduce的简单用法
- fliter用法
lst = [1,2,3,4,5,6] #筛选满足条件的元素 s = list(filter(lambda x:x%2==0,lst)) print(s)
- map的应用(1)
lst = ['123.5','111','456','789'] #列表里面的元素转数字 x = list(map(eval,lst)) #列表里面的元素转字符 y = list(map(str,x)) print(x) print(y)
- map的应用(2)
lst = ['Abc','ddsvH'] #map使用lambda转为大写 x = list(map(lambda x : x.upper(),lst)) print(x)
- reduce的用法
from functools import reduce lst = [1,2,3,4,5] #递归计算列表的和 x = reduce(lambda x,y:x+y,lst) print(x)
评论区