Discuss / Python / 仅供参考

仅供参考

Topic source

viper1090

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

def str2float(s):

DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4,

'5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

dotIdx = s.index('.')

if dotIdx > 0:

inter = s[:dotIdx]

temp = list(s[dotIdx + 1:])

temp.reverse()

floater = ''.join(temp)

print(inter)

print(floater)

result1 = reduce(lambda x, y: x * 10 + y,

map(lambda x: DIGITS[x], inter))

result2 = reduce(lambda x, y: x * 0.1 + y,

map(lambda x: DIGITS[x], floater))

return result1 + result2 * 0.1

return reduce(lambda x, y: x * 10 + y, map(lambda x: DIGITS[x], s))


  • 1

Reply