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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2187135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:59:44  来源:igfitidea点击:

range and xrange for 13-digit numbers in Python?

pythonrangenumbersxrange

提问by kame

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

range()xrange()为 10 位数字工作。但是 13 位数字呢?我在论坛里没有找到任何东西。

采纳答案by Ricardo Cárdenes

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]

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

另一种解决方案是使用itertools.islice,如文档中所建议xrange

回答by dfa

if you need enumerating integer try using itertools:

如果您需要枚举整数,请尝试使用itertools

itertools.count(1000000000000)

it should not allocate memory for a list of 1000000000000elements

它不应该为1000000000000元素列表分配内存

回答by Ramashalanka

No problems with creating the range, as long as you don't want 10**13 elements, e.g.

创建范围没有问题,只要您不想要 10**13 个元素,例如

range(10**14,10**15,10**14)

gives

[100000000000000, 200000000000000, 300000000000000, 400000000000000, 500000000000000, 600000000000000, 700000000000000, 800000000000000, 900000000000000]

回答by Ignacio Vazquez-Abrams

On 64-bit Python:

在 64 位 Python 上:

>>> xrange(9999999999999)
xrange(9999999999999)

I would not use range()for a 13-digit number. My poor machine would not be able to hold the resultant list.

我不会使用range()13 位数字。我可怜的机器将无法保存结果列表。

回答by Mark Byers

I don't think it will work. Functions like lenexpect the result to fit into a 4 byte integer, due to restrictions in the cPython implementation.

我不认为它会起作用。len由于 cPython 实现中的限制,像这样的函数期望结果适合 4 字节整数。

In Python 3.0:

在 Python 3.0 中:

>>> range(9999999999999)
range(0, 9999999999999)

It looks like it works, but...

看起来它有效,但是......

>>> len(range(9999999999999))
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    len(range(9999999999999))
OverflowError: Python int too large to convert to C ssize_t

See herefor a related question.

有关相关问题,请参见此处

回答by corn3lius

range(x) returns a list.Python lists cant contain that many elements. You should use xrange() to iterate through those digits if you need to do trillions of cycles.?

range(x) 返回一个列表。Python 列表不能包含那么多元素。如果您需要执行数万亿次循环,您应该使用 xrange() 来遍历这些数字。

回答by eswald

range() and xrange() work in recent enough versions of Python; however, in 2.5 or less you'll need to work around the int to long conversion.

range() 和 xrange() 可以在足够多的 Python 版本中使用;但是,在 2.5 或更低版本中,您需要解决 int 到 long 的转换问题。

def irange(start, stop=None, step=1):
    if stop is None:
        stop = long(start)
        num = 1L
    else:
        stop = long(stop)
        num = long(start)
    step = long(step)
    while num < stop:
        yield num
        num += step

This isn't a complete solution (it doesn't handle negative steps), but it should get you going.

这不是一个完整的解决方案(它不处理消极步骤),但它应该可以帮助您前进。

回答by Confusion

The difference between range() and xrange() is that the first returns the entire list, while the second returns a generator that generates each number as it is needed. The second one should work for any number, no matter how large.

range() 和 xrange() 之间的区别在于第一个返回整个列表,而第二个返回一个生成器,根据需要生成每个数字。第二个应该适用于任何数字,无论多大。

In Python 3.0, xrange() has disappeared and range() behaves like xrange() did previously.

在 Python 3.0 中, xrange() 已经消失,而 range() 的行为与之前的 xrange() 一样。

回答by vil

For sollution of this problem you don't need such long numbers, because you need only prime factors, you can use square root:

为了解决这个问题,你不需要这么长的数字,因为你只需要质因数,你可以使用平方根:

for i in xrange(2, int((n+1)**0.5)):