字符串從右開始取2位用什么函數?
a // 1000 % 10000看到有不少用把數轉成字符串再截取了,比如 int(str(a)[-7:-3])。行是行,但是當數很大時,轉成十進制字符串表示很慢的,看:$ python -m timeit -s 'a = 2 ** 10000' 'a // 1000 % 10000'100000 loops, best of 3: 14.6 usec per loop$ python -m timeit -s 'a = 2 ** 10000' 'int(str(a)[-7:-3])'1000 loops, best of 3: 444 usec per loop$ python -m timeit -s 'a = 2 ** 100000' 'a // 1000 % 10000'10000 loops, best of 3: 141 usec per loop$ python -m timeit -s 'a = 2 ** 100000' 'int(str(a)[-7:-3])'10 loops, best of 3: 43.2 msec per loop
上一篇計算機二級考試