简体   繁体   中英

How to print the alphabet one after the other and extend this

I have my alphabet and would like it to continue one after the other

as an example:

"a"
"b"
"c"
...

When this happens from "a" to "z" I want my code to add a letter to it.

as an Example:

"aa"
"ab"
"ac"
"ad"
...

And wanted wanted to ask if this works with variables

EDIT for comment My code (for now):

import time


char_abc = "abcdefghijklmnopqrstuvwxyz"
char_ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

for i in char_abc:
    print(i)
    time.sleep(0.1)
    
for j in char_ABC:
    print(j)
    time.sleep(0.1)

# for a in char_abc + char_ABC: #
#   print(a)                    # This is only a test
#   time.sleep(0.1)             #

to solve the misunderstanding i have deleted my sentence for the ord() function to only deal with the variables

You can create a function that yields the cartesian product of increasing lengths of the alphabet.

I use itertools.count to keep track of a counter starting at 1 and incrementing everytime the outerloop restarts. Then the inner loop is in charge of generating the actual letter combinations via itertools.product

def cycle_abc():
    char_abc = "abcdefghijklmnopqrstuvwxyz"
    for i in itertools.count(1):
            for combo in itertools.product(char_abc, repeat=i):
                    yield "".join(combo)

for letters in cycle_abc():
    print(letters)

# a
# b
# c
# ...
# aa
# ab
# ac
# ...
# ba
# bb
# bc
# ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM