python 函数式

map

list(map(str,range(10)))
Out[1]: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
def mul(x):
    return x*x
a=list(range(10))
list(map(mul,a))
Out[18]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

reduce

from functools import reduce
a=list(range(1,10))
def add(x,y):   
    return x+y
a=list(range(10))

reduce(add,a)
Out[13]: 45

filter

a=list(range(10))

def filt(x):
    return x % 2==1
b=filter(filt,a)

list(b)
Out[24]: [1, 3, 5, 7, 9]
posted @ 2022-08-19 22:59  luoganttcc  阅读(13)  评论(0编辑  收藏  举报