作业一
def normalize(name): return name[0].upper()+name[1:].lower() L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)
作业二
from functools import reduce
def prod(L): return reduce(lambda x,y:xy,L) print('3 5 7 9 =', prod([3, 5, 7, 9]))
作业三 from functools import reduce
def str2float(s): 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] def fn(x, y): return x 10 + y def fn1(x, y): return x 0.1 + y x,y = s.split('.') y = '0' + y return reduce(fn,map(char2num,x)) + reduce(fn1,map(char2num,y[::-1])) print('str2float(\'123.456\') =', str2float('123.456'))
总结:
1、字符串相加可以合并 2、split方法可以分割字符串
第三个参考了网友的代码,写出来还是很冗长
Sign in to make a reply
诶嘿诶嘿我是张宁宇
def normalize(name): return name[0].upper()+name[1:].lower() L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)
from functools import reduce
def prod(L): return reduce(lambda x,y:xy,L) print('3 5 7 9 =', prod([3, 5, 7, 9]))
def str2float(s): 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] def fn(x, y): return x 10 + y def fn1(x, y): return x 0.1 + y x,y = s.split('.') y = '0' + y return reduce(fn,map(char2num,x)) + reduce(fn1,map(char2num,y[::-1])) print('str2float(\'123.456\') =', str2float('123.456'))
1、字符串相加可以合并 2、split方法可以分割字符串