# -*- coding: utf-8 -*- from functools import reduce normalize = lambda n : str(n).title(); # 使用自带的title()方法 def prod(L): return reduce(lambda x,y:x*y,L) # lambda表达式计算乘积 print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9])) def str2float(s): try: isNeg,f = -int(s[0]=='-') or 1,list(map(lambda st: reduce(lambda x,y:int(x != '-' and x or 0)*10+int(y[0]),st),s.split('.'))) return (int(f[0])+int(f[1])/pow(10,len(s)-s.find('.')-1)) * isNeg except ValueError as err: print('Failed: Invalid String\n'+err.__str__()); except Exception as e: print(e); print('str2float(\'123.456\') =', str2float('123.456'))
Sign in to make a reply
解集SS