[Tutor] List all possible 10digit number

eryksun eryksun at gmail.com
Sat Sep 1 05:42:42 CEST 2012


On Fri, Aug 31, 2012 at 9:08 PM, Scurvy Scott <etanes.rm at gmail.com> wrote:
>
> 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,

In itertools, look at count() and islice(). An alternative to islice
in this case is takewhile().

count() works with Python long integers. For example, counter =
count(start=10**9L).

islice() is limited to sys.maxint (sys.maxsize in Python 3), so you
need to chain several together on a 32-bit platform (also 64-bit
Windows, I gather, since a C long is always 32-bit on Windows). Use a
generator expression with chain.from_iterable:

    counter = count(start=10**9L)
    slices = (islice(counter, 10**9) for i in range(10))
    nums = chain.from_iterable(slices)

Since printing performance is limited by the terminal's speed, you
probably don't need the efficiency of islice (with its machine word
size limit). Instead, you can use takewhile() with a pure Python
predicate. For example:

    counter = count(start=10**9L)
    nums = takewhile(lambda x: x < 10**10, counter)


More information about the Tutor mailing list