我想执行字典攻击,为此我需要单词列表。如何从特定长度的字符(或从最小长度到最大长度的单词长度)生成单词列表?我试过itertools.combinations_with_replacements
和itertools.permutations
,但没有用。他们没有它应该返回的所有单词列表。任何帮助都将不胜感激。谢谢。
发布于 2014-02-04 17:19:29
>>> import itertools
>>>
>>> chrs = 'abc'
>>> n = 2
>>>
>>> for xs in itertools.product(chrs, repeat=n):
... print ''.join(xs)
...
aa
ab
ac
ba
bb
bc
ca
cb
cc
要获取从最小长度到最大长度的单词:
chrs = 'abc'
min_length, max_length = 2, 5
for n in range(min_length, max_length+1):
for xs in itertools.product(chrs, repeat=n):
print ''.join(xs)
发布于 2014-02-04 17:28:53
这是一个天真的实现:
list='abcdefg'
depth=8
def generate(l,d):
if d<1:
return
for c in l:
if d==1:
yield c
else:
for k in generate(l,d-1):
yield c+k
for d in range(1,depth):
for c in generate(list,d):
print c
我还没有足够的名气来发表评论,所以,根据上面的itertools示例制作一个完整的列表:
import itertools
chrs='abc'
n=6
for i in range(1,n):
for xs in itertools.product(chrs, repeat=i):
print ''.join(xs)
这样,你的列表中就有了长度从1到n的所有单词。
发布于 2014-02-04 17:23:30
from itertools import product
def allwords(chars, length):
for letters in product(chars, repeat=length):
yield ''.join(letters)
def main():
letters = "abc"
for wordlen in range(3, 5):
for word in allwords(letters, wordlen):
print(word)
if __name__=="__main__":
main()
返回
aaa
aab
aac
aba
abb
...
ccbc
ccca
cccb
cccc
https://stackoverflow.com/questions/21559039
复制相似问题