[三个版本]自定义int()函数(Python实现)
生活随笔
收集整理的這篇文章主要介紹了
[三个版本]自定义int()函数(Python实现)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼一:
from functools import reduce def int(string):def f(x, y):return x*10 + ydef m(c):return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,'9': 9}[c]return reduce(f, map(m, string))
代碼二:
from functools import reduce def int(string):def f(x, y):return x*10 + yreturn reduce(f, map(lambda x: ord(x) - ord('0'), string))
代碼三:
from functools import reduce def int(string):return reduce(lambda x, y: x*10 + y, map(lambda x: ord(x) - ord('0'), string))
總結
以上是生活随笔為你收集整理的[三个版本]自定义int()函数(Python实现)的全部內容,希望文章能夠幫你解決所遇到的問題。