1.利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:[‘adam’, ‘LISA’, ‘barT’],输出:[‘Adam’, ‘Lisa’, ‘Bart’]:
def normalize(name):
lower = name.lower()
return lower.title()
2.Python提供的sum()函数可以接受一个list并求和,请编写一个prod()函数,可以接受一个list并利用reduce()求积:
from functools import reduce
def prod(L):
return reduce(mul,L)
def mul(x,y):
return x * y
3.利用map和reduce编写一个str2float函数,把字符串’123.456’转换成浮点数123.456:
from functools import reduce
def str2float(s):
def number_int(s):
numbers = {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
'9': 9}
return numbers[s]
def flt(x, y):
return x * 10 + y
return reduce(flt, map(number_int, s.replace('.', ''))) / (10 ** (len(s) -
s.in