Python中13位数字的范围和xrange?

2022-01-17 00:00:00 python numbers range xrange

问题描述

range()xrange() 适用于 10 位数字.但是 13 位数字呢?我在论坛中没有找到任何东西.

range() and xrange() work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.


解决方案

你可以试试这个.与范围相同的语义:

You could try this. Same semantics as range:

import operator
def lrange(num1, num2 = None, step = 1):
    op = operator.__lt__

    if num2 is None:
        num1, num2 = 0, num1
    if num2 < num1:
        if step > 0:
            num1 = num2
        op = operator.__gt__
    elif step < 0:
        num1 = num2

    while op(num1, num2):
        yield num1
        num1 += step

>>> list(lrange(138264128374162347812634134, 138264128374162347812634140))
[138264128374162347812634134L, 138264128374162347812634135L, 138264128374162347812634136L, 138264128374162347812634137L, 138264128374162347812634138L, 138264128374162347812634139L]

另一个解决方案是使用 itertools.islice,正如 xrange 的 文档

Another solution would be using itertools.islice, as suggested inxrange's documentation

相关文章