python基础:高阶函数,map,reduce,filter,sorted

map

用法:map(func,序列),作用:将func一一作用在序列的元素上,并返回一个序列

举例:map(func,[1,2,3])=[func(1),func(2),func(3)]

reduce

用法:reduce(func,序列),作用:将func累计作用在序列上;注意:reduce中的func,必须接收2个参数,如func(x,y)

举例:reduce(func,[1,2,3])=func([1,2],3)=func(func(func(1,2)),3)

filter

用法:filter(func,序列),作用:将func一一作用在序列的元素上,根据返回值为True或False决定保留还是丢弃元素;需要用list(filter(func,序列)),需要用list()获取结果并返回list

举例:

def getodd(self,list1):

  return list(filter(self.is_odd,list1))

sorted

用法:对列表进行排序,如sorted(list)

原文地址:https://www.cnblogs.com/jinziguang/p/14886079.html