内置函数,重要的四个reduce,map,lambda,filter

时间:2022-09-03 19:30:46
#filter过滤器
#filter(函数,列表)#把列表里的元素序列化,然后在函数中过滤
# str=["a","b","c","d"]
# def funl(s):
# if s !="a":
# return s
# ret=filter(funl,str)
# print(ret)#<filter object at 0x0000015C80478278>迭代器对象,
# print(list(str))#把迭代器强制转换成列表



# #map(函数,列表)
# str=["a","b","c","d"]
# def fun2(s):
# return s+"abster"
# ret=map(fun2,str)
# print(ret)#跟filter一样,也是生成迭代器,不过filter是过滤,不改变列表元素
# print(list(ret))#['aabster', 'babster', 'cabster', 'dabster']




# #reduce(函数,序列)
# from functools import reduce
# def add1(x,y):
# return x+y
# print(reduce(a



#lambda无名函数
# lambda x,y:x*y #先写出需要带人的量,后面是处理方法