Discuss / Python / func

from functools import reduce s='-123.456' dict={'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'0':0}

def fun_1(s): return dict[s]

def fun_2(x,y): return 10*x+y

def fun_3(x,y): return 0.1x+y flag=0 if not s[0].isdigit(): flag=1 iter_1=map(fun_1,s[flag:s.index('.')]) iter_2=map(fun_1,(s[s.index('.')+1:])[: :-1]+'0') a=1 if s[0]=='-': a=-1 print(a(reduce(fun_2,iter_1)+reduce(fun_3,iter_2)))


  • 1

Reply