[Tutor] List all possible 10digit number

Dave Angel d at davea.name
Sat Sep 1 04:01:01 CEST 2012


On 08/31/2012 09:08 PM, Scurvy Scott wrote:
> First of all thank you guys for all your help. The manual is really no substitute for having things explained in laymans terms as opposed to a technical manual.
>
> My question is this- I've been trying for a month to generate a list of all possible 10 digit numbers. I've googled, looked on stackoverflow, experimented with itertools, lists, etc to no avail. The closest I've gotten is using itertools to generate every possible arrangement of a list
>
> List = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>
> I feel like I'm close but can't quite get it. Any suggestions or shoves in the right direction would be helpful. An issue I've encountered is that python won't do something like
>
> For I in range(1000000000, 9999999999):
>     Print I
>
> Without crashing or throwing an exception.
>
> I've also tried to do something like
>
> I in range (100, 900):
>    Etc
> And then concatenate the results but also to no avail.
>
> Again, any shoves in the right direction would be greatly appreciated.
>

You should have mentioned that you're using Python 2.x.  (Things are
different in 3.x )

The range() function is the wrong answer, since 9 billion integers
aren't going to fit in memory, on most machines.   You should at least
have tried xrange().  Unfortunately, it's limited to ints, which only go
to a couple billion.  That's probably why your assignment specifies 10
digits.

The easy answer is to do a while loop that's equivalent to the xrange().
Initialize your counter to the smallest value you're interested in, and
each time through the loop, print the counter and increment it.  The
loop should terminate when you pass the highest value of interest.

The "right" answer is to write a generator, using yield to yield each
value.  Interestingly, it's the same loop as previous, but it yields
instead of prints.
Then, once you have the generator, you write a loop using it, rather
than xrange.


-- 

DaveA



More information about the Tutor mailing list