简体   繁体   English

如何一个接一个地打印字母表并扩展它

[英]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.当从“a”到“z”发生这种情况时,我希望我的代码向其中添加一个字母。

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为了解决误解我删除了我对 ord() 函数只处理变量的句子

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.我使用itertools.count来跟踪从 1 开始并在每次外循环重新启动时递增的计数器。 Then the inner loop is in charge of generating the actual letter combinations via itertools.product然后内循环负责通过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
# ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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