目录
map使用:
result = map(func,iter)
等同于:
def func(value):
# pro value or gen subresult
return subresult
result = []
for value in iter:
result.append(func(value))
reduce的使用:
result = reduce(func,iter) #其中可迭代对象iter的value值大于两个
等同于:
def func(value1,value2):#一定是两个输入
# pro value or gen subresult
return subresult
result = iter[0]
for value2 in iter[1:end]:
result = func(result ,value2)
使用示例:
字符转数字:
from functools import reduce
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def char2num(s):
return DIGITS[s]
def str2int(s):
return reduce(lambda x, y: x * 10 + y, map(char2num, s))
sa = '686520587'
a = str2int(sa)
print(a)
b=int(sa)
print(b)
运行结果:
参考:https://www.liaoxuefeng.com/wiki/1016959663602400/1017329367486080