关于作者

Discuss / Python / 交作业啦

交作业啦

Topic source

逍遥的老蒋

#1 Created at ...

1.利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。 def normalize(s): r=s.lower() return r.capitalize() L1 = ['adam', 'LISA', 'barT'] print(list(map(normalize,L1)))

2.请编写一个prod()函数,可以接受一个list并利用reduce()求积: from functools import reduce def prod(L): def chengji(x,y): return xy return reduce(chengji,L) print('3 5 7 9 =', prod([3, 5, 7, 9]))

3.利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456: from functools import reduce def str2float(r): def char2num(r): return {'1':1,'2':2,'3':3,'4':4,'5':5,'6':6}[r] r=r.split('.') r1=reduce(lambda o,p:o10+p,map(char2num,r[0])) r2=reduce(lambda o,p:o10+p,map(char2num,r[1]))/(10**len(r[1])) return r1+r2 print('str2float(\'123.456\') =', str2float('123.456'))


  • 1

Reply

WARNING: You are using an old browser that does not support HTML5. Please choose a modern browser (Chrome / Microsoft Edge / Firefox / Sarafi) to get a good experience.