python实现数字字符串转换为数字

python学习网 2017-12-06 11:52:01
 1 # '78695' -> 78695
 2 from functools import reduce
 3 
 4 
 5 def str_to_num(num_str):
 6     num_dict = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
 7     return reduce(lambda x, y: x * 10 + y, list(map(lambda x: num_dict[x], num_str)))
 8 
 9 
10 if __name__ == '__main__':
11     print(str_to_num('78695'))

 

阅读(772) 评论(0)