Discuss / Python / 交作业

交作业

Topic source

狗不理翔

#1 Created at ... [Delete] [Delete and Lock User]

第一题:

def normalize(name): return map(lambda x:x.lower().capitalize(),name)

第二题:

from functools import reduce def prod(L): return reduce(lambda x,y:x*y,L)

第三题:

from functools import reduce def str2float(s): def charnum(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(lambda x,y:x10+y,map(charnum,s.replace('.',''))))/(10**(len(s)-s.find('.')-1))

狗不理翔

#2 Created at ... [Delete] [Delete and Lock User]

最后一个return语句是x*10+y,少了一个乘号,不知道为什么没有显示出来。

totoroma

#3 Created at ... [Delete] [Delete and Lock User]

from functools import reduce def prod(L): def f(x,y): return x*y return reduce(f,L)

print('357*9=',prod([3,5,7,9]))

我是这样做的,请问下这样为什么可以不用定义L是一个list?比如L=【】?


  • 1

Reply