我正在尝试创建一个单词列表生成器,该生成器创建一个包含所有可能的大写字母和数字组合的文件,但采用这种非常特定的格式:
AAA00AA(大写、大写、大写、数字、数字、大写、大写(
因此,第一个字符串将AAA00AA,最后一个字符串ZZZ99ZZ。有超过 10 亿种可能的组合,我正在使用 itertools.product
函数。
但是,我坚持如何循环浏览每次迭代的结果,以便让每个组(AAA 00 AA(相互组合。这是我到目前为止得到的,但每个循环只运行一次。例如,当第一组 AAA 00 AA 达到 ZZZ 00 AA 时,我需要让第二组通过 1 次迭代到 AAA 01 AA,依此类推,直到第三组。
我确定我的循环嵌套逻辑是错误的,或者我可能需要使用其他方法,但我不知道该怎么做。请问谁能帮忙?这是我到目前为止的代码。
import string
import itertools
import datetime
letters = string.ascii_uppercase
digits = string.digits
first_group = itertools.product(letters, repeat=3)
second_group = itertools.product(digits, repeat=2)
third_group = itertools.product(letters, repeat=2)
FILE = open("mylist.txt","w")
start = datetime.datetime.now()
for i in first_group:
first = ''.join(i)
FILE.write(first + 'n')
for a in second_group:
second = first +''.join(a)
FILE.write(second + 'n')
for x in third_group:
string = second +''.join(x)
FILE.write(string + 'n')
string = ''
FILE.close()
print 'DONE! - Finished in %s' % (datetime.datetime.now() - start)
itertools.product
再次加入子产品。
f, s, t = [
itertools.product(d, repeat=r)
for d, r in zip([letters, digits, letters], [3, 2, 2])
]
with open("mylist.txt", "w") as f:
for prod in itertools.product(f, s, t):
string = ''.join([''.join(k) for k in prod])
f.write(string + 'n')
# AAA00AA
# AAA00AB
# AAA00AC
# AAA00BA
# AAA00BB
# .......
import string
import itertools
import datetime
letters = string.ascii_uppercase
digits = string.digits
first_group = itertools.product(letters, repeat=3)
second_group = itertools.product(digits, repeat=2)
third_group = itertools.product(letters, repeat=2)
start = datetime.datetime.now()
with open("mylist.txt","w") as FILE:
for i in first_group:
first = ''.join(i)
for j in second_group:
second = ''.join(j)
for k in third_group:
FILE.write(first + second + ''.join(k) + 'n')
print 'DONE! - Finished in %s' % (datetime.datetime.now() - start)
生成:
您可以AAA00AA
AAA00AB
AAA00AC
AAA00AD
AAA00AE
AAA00AF
。
保持原样的其他一切。 然而,@Coldspeed的itertools.product
解决方案更优雅,也可能更快。我只是想纠正你的代码。
使用列表推导:
res = ["".join(itertools.chain(a,b,c)) for c in third_group for b in second_group for a in first_group]
res
['AAA00AA', 'AAB00AA', 'AAC00AA', 'AAD00AA', 'AAE00AA', 'AAF00AA', 'AAG00AA', 'AAH00AA', 'AAI00AA', 'AAJ00AA', 'AAK00AA', 'AAL00AA', 'AAM00AA', 'AAN00AA', 'AAO00AA', 'AAP00AA', 'AAQ00AA', 'AAR00AA', 'AAS00AA', 'AAT00AA', 'AAU00AA', 'AAV00AA', 'AAW00AA', 'AAX00AA', 'AAY00AA',...]
您甚至可以将其设为生成器对象:
for e in ("".join(itertools.chain(a,b,c)) for c in third_group for b in second_group for a in first_group):
print e