函数式编程的一个特点:允许把函数本身作为参数传入另一个函数,还允许返回一个函数
高阶函数
函数A接受函数B作为参数,把函数作为参数传入
一、map()
接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回
def f(x):
return x * x
if __name__ == '__main__':
r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(list(r)) # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
练习题:利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字
输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']
def normalize(name):
name = name[0].upper() + name[1:].lower()
return name
if __name__ == '__main__':
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)
二、 reduce()
把一个函数作用在一个序列[x1, x2, x3, ...]上,必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算
from functools import reduce
def add(x, y):
return x + y
def fn(x, y):
return x * 10 + y
def char2num(s):
digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
return digits[s]
if __name__ == '__main__':
a = reduce(add, [1, 3, 5, 7, 9])
print(a) # 25
b = reduce(fn, [1, 3, 5, 7, 9])
print(b) # 13579
c = reduce(fn, map(char2num, '1357924681012564968596'))
print(c) # 13579
练习题:编写一个prod()函数,可以接受一个list并利用reduce()求积
from functools import reduce
def prod(L):
# return reduce(lambda x, y: x * y, L)
def fn(x, y):
return x * y
return reduce(fn, L)
if __name__ == '__main__':
print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))
if prod([3, 5, 7, 9]) == 945:
print('测试成功!')
else:
print('测试失败')
综合练习
利用map和reduce编写一个str2float函数,把字符串'123.4567'转换成浮点数123.4567
from functools import reduce
def str2float(s):
def fn(x, y):
return x * 10 + y
n = s.index('.')
s1 = list(map(int, [x for x in s[:n]])) # [1, 2, 3]
s2 = list(map(int, [x for x in s[n+1:]])) # [4, 5, 6, 7]
return reduce(fn, s1) + reduce(fn, s2)/10**len(s2)
if __name__ == '__main__':
print('\'123.4567\'=', str2float('123.4567'))
方法二:
from functools import reduce
def str2float(s):
def fn(x, y):
return x * 10 + y
def char2num(s):
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
return reduce(fn, map(char2num, s.replace(".","")))
s="1234.567"
if s.find(".")!=-1:
print('str2float(\'%s\') ='%s, str2float(s)/pow(10,(len(s)-s.find(".")-1)))
else:
print('str2float(\'%s\') ='%s, str2float(s))
网友评论