Python map()-reduce() 把字符串转换成浮点数

mac2022-06-30  30

1 import re 2 from functools import reduce 3 4 s = '1546.322222' 5 def ss(x): 6 sss = s.split('.') 7 return sss[x] 8 9 def str2num(x): 10 return int(x) 11 12 13 def m(): 14 L10 = list(map(str2num,ss(0))) 15 L01 = list(map(str2num,ss(1))) 16 L = reduce(lambda x,y:x*10+y,L10)+reduce(lambda x,y:x*10+y,L01)*0.1**len(L01) 17 return L 18 19 print(m())

关键是点的判断,用了re。

转载于:https://www.cnblogs.com/nero3/p/7728470.html

最新回复(0)