How to iterate over alpha and numeric numbers

I would like to know how in Python I can iterate through a set of conditions.

  • which has 2-6 lower alphabetic or numeric characters
  • the first character is always a number

So the short progression will be:

1a
1b
1c
...
1aa
1ab
1ac
...
2aaa
2aab
2aac

etc.

      

A terrible example that the first two can do is

##Loop through 1a-z0-9
start = '1'
l = 97
while l < 123:
    num = start
    num += chr(l)
    print num
    l += 1

l = 48
while l < 58:
    num = start
    num += chr(l)
    print num
    l += 1

      

I found itertools but can't find good examples to get away with.

+3


source to share


4 answers


You can do this using itertools.product

and itertools.chain

. First, define strings of numbers and letters:

numbers = '0123456789'
alnum = numbers + 'abcdefghijklmnopqrstuvwxyz'

      

Using itertools.product

, you can get character tuples for strings of different lengths:



len2 = itertools.product(numbers, alnum) # length 2
len3 = itertools.product(numbers, alnum, alnum) # length 3
...

      

Concatenate iterators for all lengths together by appending tuples to strings. I would do it with a list:

[''.join(p) for p in itertools.chain(len2, len3, len4, len5, len6)]

      

+4


source


I would go with a product feature from itertools.



import itertools 
digits = '0123456789'
alphanum = 'abcdef...z' + digits # this should contain all the letters and digits

for i in xrange(1, 6):    
    for tok in itertools.product(digits, itertools.product(alphanum, repeat=i)):
        # do whatever you want with this token `tok` here.

      

+3


source


You can think of this problem in base 26 (ignoring the first number, we'll put this in a separate case.) So the letters we want to vary from 'a' to 'zzzzz' in base 26 would be 0 and (26.26 , 26,26,26) = 26 ^ 0 + 26 + 26 ^ 2 + 26 ^ 3 + 26 ^ 4 + 26 ^ 5. So now we have a bijection from numbers to letters, we just want to write a function that returns us from number to word

 letters = 'abcdef..z'

 def num_to_word( num ):
      res = ''
      while num:
           res += letters[num%26]
           num //= 26
      return res

      

Now, to write our function that lists this

 def generator():
     for num in xrange(10):
         for letter_num in xrange( sum( 26 ** i for i in xrange( 6 ) ) + 1 ):
             tok = str(num) + num_to_word( letter_num )
             yield tok

      

0


source


allows you to do this using the first type of width search algorithm

starting from 
Root:
    have 10 children, i = 0,1,...,9
    so , this root must have an iterator, 'i'
    therefore this outermost loop will iterate 'i' from 0 to 9

i:
    for each 'i', there are 5 children (ix , ixx, ixxx, ixxxx, ixxxxx)
    ( number of chars at the string )
    so each i should have its own iterator 'j' representing number of chars
    the loop inside Root loop will iterate 'j' from 1 to 5

j:
    'j' will have 'j' number of children ( 1 -> x , 2 -> xx ,..., 5-> xxxxx)
    so each j will have its own iterator 'k' representing each "character"
    so, 'k' will be iterated inside this j loop, from 1 to j
    ( i=2, j=4, k = 3 will focus on 'A' at string  "2xxAx" )

k:
    each 'k' represents a character, so it iterates from 'a' to 'z'
    each k should have a iterator(value) 'c' that iterates from 'a' to 'z' (or 97 to 122)

      

I think it will make more sense than what I wanted to show before. :) if you don't get this idea please tell me .. Interesting question by the way :)

-1


source







All Articles