python - Pythonic way of writing a sequence of integers in monotonically increasing order -


what pythonic , efficient way of writing integers 1 10**6 digits in monotonically increasing order?

for example: (1,2,3,4,5,6,7,8,9,10,11,20,21,22,30,31,32,33,...)

this gets job done looks pretty ugly.

nums = [10**0*k6 k6 in range(1,10)] +        [10**1*k5 + 10**0*k6 k5 in range(1,10) k6 in range(k5+1)] +         [10**2*k4 + 10**1*k5 + 10**0*k6          k4 in range(1,10) k5 in range(k4+1) k6 in range(k5+1)] +        [10**3*k3 + 10**2*k4 + 10**1*k5 + 10**0*k6         k3 in range(1,10) k4 in range(k3+1) k5 in range(k4+1) k6 in range(k5+1)] +        [10**4*k2 + 10**3*k3 + 10**2*k4 + 10**1*k5 + 10**0*k6         k2 in range(1,10) k3 in range(k2+1) k4 in range(k3+1) k5 in range(k4+1) k6 in range(k5+1)] +        [10**5*k1 + 10**4*k2 + 10**3*k3 + 10**2*k4 + 10**1*k5 + 10**0*k6         k1 in range(1,10) k2 in range(k1+1) k3 in range(k2+1) k4 in range(k3+1) k5 in range(k4+1) k6 in range(k5+1)] 

this returns 8001 numbers max_digits=6:

def ascending(ndig, first_digit_max):     x in xrange(0, first_digit_max+1):         if ndig == 1:             yield [x]         else:             y in ascending(ndig-1, x):                 yield [x] + y  max_digits = 6 nums = sorted([int(''.join(map(str, num)))                ndig in xrange(1, max_digits+1)                num in ascending(ndig, 9)                if any(num)]) 

ascending yields lists of ndig digits, first digit lower or equal first_digit_max. works recursively, if called ndig=6, calls ndig=5, etc. until calls ndig=1 returns individual digits. these lists, have checked if of these digits different 0 (otherwise return 0, 00, 000, etc. well) , converted numbers.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -