所以,我想学习如何使用以下格式创建所有可能组合的列表:
AA0AA0A
它需要像这样,AZ 0-9,但保持字符的位置如上所示。
使用生成器
注意:根据@ShadowRange、@Kelly Bundy、@superb rain 的建议更新了答案)
from string import ascii_uppercase, digits
from itertools import product
def all_combinations():
' pattern AA0AA0A '
A, O = ascii_uppercase, digits
# Use itertools product to generate all combinations of of
# lists of letters and digits
combs = product(A, A, O, A, A, O, A)
return map(''.join, combs)
# Test
seq = all_combinations() # create generation for combinations
# Show first 20
for i in range(20):
print(next(seq))
输出
AA0AA0AA
AA0AA0AB
AA0AA0AC
AA0AA0AD
AA0AA0AE
AA0AA0AF
AA0AA0AG
AA0AA0AH
AA0AA0AI
AA0AA0AJ
AA0AA0AK
AA0AA0AL
AA0AA0AM
AA0AA0AN
AA0AA0AO
AA0AA0AP
AA0AA0AQ
AA0AA0AR
AA0AA0AS
AA0AA0AT
这是可能的,有 976,562,500 种可能性……
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ["0","1","2","3","4","5","6","7","8","9"]
combination = []
x = 0
for a in letters:
for b in letters:
for c in numbers:
for d in letters:
for e in letters:
for f in numbers:
for g in letters:
combination.append(a+b+c+d+e+f+g)
小心 !此计算可能需要一段时间。如果您没有超过 16BG 的 RAM,也不要尝试运行此脚本。