开发者

python for loop range(bigint)

开发者 https://www.devze.com 2023-01-05 20:40 出处:网络
In Python, is there some short way to do something like \"for i in range(n)\" when n is too big for Python to actually create the array range(n)?

In Python, is there some short way to do something like

"for i in range(n)"

when n is too big for Python to actually create the array range(n)?

(short because otherwise I'd just us开发者_如何学编程e a while loop)


You could use xrange()... although that is restricted to "short" integers in CPython:

CPython implementation detail: xrange() is intended to be simple and fast. Implementations may impose restrictions to achieve this. The C implementation of Python restricts all arguments to native C longs (“short” Python integers), and also requires that the number of elements fit in a native C long. If a larger range is needed, an alternate version can be crafted using the itertools module: takewhile(lambda x: x<stop, (start+i*step for i in count())).

I don't know whether that restriction also applies to other implementations (or which ones) - but there's a workaround listed...

I know you mention bigint in your question title, but the question body talks about the number being too big to create the array - I suspect there are plenty of numbers which are small enough for xrange to work, but big enough to cause you memory headaches with range.


I would use a generator function: example forthcoming.

def gen():
    i = 0
    while 1: # or your special terminating logic
        yield i
        i = i + 1


for j in gen():
    do stuff


You could upgrade to python3. There, range isn't limited to 'short' integers.

Another workaround would be to use xrange for small integers and add them to some constant inside the loop, e.g.

offset, upperlimit = 2**65, 2**65+100
for i in xrange(upperlimit-offset):
    j = i + offset
    # ... do something with j


you should always use xrange rather than range for just looping n times over sth., but keep in mind that xrange has also a limit (if it is too small you need to do your own while loop with a counter)

EDIT: too late...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号