第三题的一种尝试
Topic source终于在网上找到了对应的讲解:
lst = [1,2,3,4,5]
print reduce(lambda x,y:x+y,lst,0)
这种方式用lambda表示当做参数,因为指定了reduce的第三个参数为0,所以第一次执行时x=0,y=1,第二次x=0(x1)+1(y1),y=2
基于此改了一下
def str2float(s):
s_sp = s.split('.')
return reduce(lambda x, y : x*10 + y,map(int, s_sp[0])) \
+ reduce(lambda x, y : x/10 + y,map(int, s_sp[1][::-1]))/10
大体上都差不多。
def str2float(s):
temp_s = s if '.' in s else s+'.'
d, f = temp_s.split('.')
return reduce(lambda x, y : x*10 + y, map(int, d), 0) \
+ reduce(lambda x, y : x/10 + y,map(int, f[::-1]), 0)/10
- 1
tabbar