本文介绍了顺序生成字母数字字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个循环来生成和打印字符串,如下所示:
I'm trying to create a loop to generate and print strings as follows:
- 仅字母数字字符:
- 0-9在AZ之前,在z之前,
- 长度最多增加4个字符。
因此,它将打印:
- 所有0-z字符串
- 然后从00-zz
- 然后从000-zzz
- 然后从0000-zzzz
- all strings from 0-z
- then from 00-zz
- then from 000-zzz
- then from 0000-zzzz
然后停止。
推荐答案
from string import digits, ascii_uppercase, ascii_lowercase
from itertools import product
chars = digits + ascii_uppercase + ascii_lowercase
for n in range(1, 4 + 1):
for comb in product(chars, repeat=n):
print ''.join(comb)
首先创建一个由所有数字,大写字母和小写字母组成的字符串。
This first makes a string of all the numbers, uppercase letters, and lowercase letters.
然后,从1-开始,每个长度4,打印所有可能的c
Then, for each length from 1-4, it prints every possible combination of those numbers and letters.
请记住,这是很多组合-62 ^ 4 + 62 ^ 3 + 62 ^ 2 + 62。
Keep in mind this is A LOT of combinations -- 62^4 + 62^3 + 62^2 + 62.
这篇关于顺序生成字母数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!