Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate a to zzz in python

So I need to get a function that generates a list of letters that increase from a, and end in zzz.

Should look like this:

a
b
c
...
aa
ab
ac
...
zzx
zzy
zzz

The code I currently have is this:

for combo in product(ascii_lowercase, repeat=3):
            print(''.join(combo))

However, this does only increase with 3 letters, and the output is more like

a
ab
abc
abcd
...

So, to recap: Function that letters increase, and when it goes past z, it returns to aa. Thanks!


UPDATE:

I am having the same output as before. Here is what I am trying to plug it into:

a = hashlib.md5()
for chars in chain(ALC, product(ALC, repeat=1), product(ALC, repeat=1)):
    a.update(chars.encode('utf-8'))
    print(''.join(chars))
    print(a.hexdigest())

My hash ends up like:

f1784031a03a8f5b11ead16ab90cc18e

but I expect:

415290769594460e2e485922904f345d

Thanks!

like image 986
user3642145 Avatar asked May 15 '14 18:05

user3642145


People also ask

How do you increment alphabet in Python?

To increment a character in a Python, we have to convert it into an integer and add 1 to it and then cast the resultant integer to char. We can achieve this using the builtin methods ord and chr.

How to iterate Over text in Python?

One way to iterate over a string is to use for i in range(len(str)): . In this loop, the variable i receives the index so that each character can be accessed using str[i] .

Can we iterate list in Python?

We can iterate through a list by using the range() function and passing the length of the list. It will return the index from 0 till the end of the list.


1 Answers

Add another loop:

for x in range(1, 4):
    for combo in product(ascii_lowercase, repeat=x):
        print(''.join(combo))

Output is as follows:

a
...
aa
...
aaa
...
zzz

Where ... is a huge number of combinations.

like image 184
vaultah Avatar answered Sep 29 '22 00:09

vaultah