我正在尝试创建一个wordlist生成器,它创建一个包含所有可能的大写字母和数字组合的文件,但是这种格式非常具体:
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)
答案 0 :(得分:3)
您可以使用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
# .......
答案 1 :(得分:0)
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
解决方案更优雅,也可能更快。我只是想纠正你的代码。
答案 2 :(得分:0)
使用列表理解:
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