第三题,适用于所有字符串转化数字
Topic sourcereturn {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] 最后面的[s] 是什么意思啊?请问
- 1
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] 最后面的[s] 是什么意思啊?请问
乱雨打新荷
def str2float(s): def str2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] def fn1(x,y): return x10+y c=s.index('.') d=len(s) g=d-c-1 return (reduce(fn1,map(str2num,s[:c])))+pow(10,-g)reduce(fn1,map(str2num,s[c+1:]))