from functools import reduce
def str2float(s):
def str2int(s):
chrList = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'.': -1,
}
return chrList[s]
str = '123.456'
nums = list(map(str2int, str))
# print('nums =', nums)
point = 0
def f(x, y):
nonlocal point
if y == -1:
point = 1
if point == 0:
# print(x * 10 + y)
return x * 10 + y
elif y == -1:
# print(x + 0.0)
return x + 0.0
else:
point *= 10
# print((x + y / point))
return (x + y / point)
reduce(f,nums)
Sign in to make a reply
王胜辉仔
from functools import reduce
def str2float(s):
def str2int(s):
chrList = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'.': -1,
}
return chrList[s]
str = '123.456'
nums = list(map(str2int, str))
# print('nums =', nums)
point = 0
def f(x, y):
nonlocal point
if y == -1:
point = 1
if point == 0:
# print(x * 10 + y)
return x * 10 + y
elif y == -1:
# print(x + 0.0)
return x + 0.0
else:
point *= 10
# print((x + y / point))
return (x + y / point)
reduce(f,nums)