Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

range and xrange for 13-digit numbers in Python?

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

like image 533
kame Avatar asked Feb 02 '10 19:02

kame


2 Answers

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

like image 133
Ricardo Cárdenes Avatar answered Sep 27 '22 18:09

Ricardo Cárdenes


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]
like image 36
Ramashalanka Avatar answered Sep 27 '22 18:09

Ramashalanka