# -*- coding: utf-8 -*
# 高阶函数
from functools import reduce
def f(x):
return x*x
L=[1,2,3,4,5,6,7]
r=map(f,L)
print(list(r))
r=map(str,L)
print(list(r))
def add(x,y):
return x+y
print(reduce(add,L))
# 同内置函数sum 的功能能
#将L变成 1234567
def fu(x,y):
return x*10+y
print(reduce(fu,L))
# 将str2int
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def str2int(s):
def fn(x, y):
return x * 10 + y
def char2num(s):
return DIGITS[s]
return reduce(fn, map(char2num, s))
print(str2int('136'))
#练习利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:['adam', 'LISA'
, 'barT'],输出:['Adam', 'Lisa', 'Bart']
def normarized(s):
return s[0].upper()+s[1:].lower()
print(normarized('wwL'))
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normarized, L1))
print(L2)