views:

372

answers:

8

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

A: 

On 64-bit 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.

Ignacio Vazquez-Abrams
I get 'OverflowError: long int too large to convert to int' on Python 2.5 if I try this.
Mark Byers
Interesting. I'm running 2.6 here. Updated.
Ignacio Vazquez-Abrams
I get `OverflowError` in 2.6.2.
MAK
Ah, maybe it's because I'm running 64-bit Python then.
Ignacio Vazquez-Abrams
A: 

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

In 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 here for a related question.

Mark Byers
+3  A: 

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

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

gives

[100000000000000, 200000000000000, 300000000000000, 400000000000000, 500000000000000, 600000000000000, 700000000000000, 800000000000000, 900000000000000]
Ramashalanka
This doesn't work with xrange, which only works on 32-bit numbers (on 32-bit systems).
Glenn Maynard
That's true. My system is 64-bit, so xrange only works up to sys.maxint = 2^63-1 approx 10^19.
Ramashalanka
A: 

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.?

corn3lius
A: 

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.

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.

eswald
+1  A: 

if you need enumerating integer try using itertools:

itertools.count(1000000000000)

it should not allocate memory for a list of 1000000000000 elements

dfa
+2  A: 

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

Heim
+1 for `itertools.islice` reference
J.F. Sebastian
A: 

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.

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

Confusion
`xrange` doesn't work for arbitrarily-large numbers.
Mike Graham