Discuss / Python / 练习题3

练习题3

Topic source

曦冶程

#1 Created at ... [Delete] [Delete and Lock User]
在此插入代码

def char2num(char): return {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '.':'.', '-':'-'}[char]

def str2float(str):

list_num = list(map(char2num, str))

if '-' in str:
    return reduce(lambda x,y: x*10+y, [x for x in list_num if x not in ['.', '-']])/-10.0**(str[::-1].find('.'))
else:
    return reduce(lambda x,y: x*10+y, [x for x in list_num if x != '.'])/10.0**(str[::-1].find('.'))

  • 1

Reply