zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python 函数式

Python 函数
2023-09-14 09:09:28 时间

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]