Discuss / Python / find函数找索引

find函数找索引

Topic source

人人仁勇

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

DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

def str2float(s): def fn(x,y): return x*10+y

i=s.find('.')

def digit(s):
    return DIGITS[s]

return reduce(fn, map(digit,s[0:i]))+reduce(fn,map(digit,s[i+1:]))/10**(len(s)-i-1)

print(str2float('5789.564'))


  • 1

Reply