[Tutor] Indexing sequences with long integers
Tim Peters
tim_one@email.msn.com
Tue, 18 May 1999 00:46:07 -0400
[Jon Cosby]
> Hi - Is there any way to index sequences with long integers?
No, at least none of the builtin sequences. If know the longs have
reasonably small values, you can do sequence[int(the_long)] though (which
will raise an exception if the long is too big to fit in an int).
> I see you can't multiply sequences with long values, or assign them to
> the range function. I'd like to be able to apply the following code,
>
> for i in range(min, max, 2):
> if primeTest(i) == 1:
> pl.append(i)
> return pl
>
> to long values for min amd max. Any suggestion?
i = min
while i < max:
...
i = i + 2
or
for offset in range(0, int(max - min), 2):
i = min + offset
...
If max-min isn't small enough to fit in an int, you'll die long before the
program could have completed anyway <wink>.
Or you could define your own sequence type, like:
class LongRange:
def __init__(self, min, max, step):
if step == 0:
raise ValueError("LongRange step must not be zero.")
self.min = min
self.max = max
self.step = step
def __getitem__(self, i):
this = self.min + i * self.step
if self.step > 0:
if this < self.max:
return this
else:
if this > self.max:
return this
raise IndexError
for i in LongRange(1000000000000000L, 1000000000000010L, 2):
print i
longingly y'rs - tim