本文介绍了Python - 如何从特定长度的给定字符生成词表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想执行字典攻击,为此我需要单词列表.如何从特定长度的给定字符(或从最小长度到最大长度的单词长度)生成单词列表?我尝试过 itertools.combinations_with_replacements
和 itertools.permutations
,但没有帮助.他们没有它应该返回的所有单词列表.任何帮助将不胜感激.谢谢.
解决方案
>>>导入迭代工具>>>>>>chrs = 'abc'>>>n = 2>>>>>>对于 itertools.product(chrs, repeat=n) 中的 xs:... 打印 ''.join(xs)...aaAB交流电巴bb公元前钙CB抄送从最小长度到最大长度获取单词:
chrs = 'abc'min_length, max_length = 2, 5对于范围内的 n(min_length,max_length+1):对于 itertools.product(chrs, repeat=n) 中的 xs:打印 ''.join(xs)
I want to perform a dictionary attack and for that I need word lists. How to generate word list from given characters of specific length ( or word length from min length to max length )? I have tried itertools.combinations_with_replacements
and itertools.permutations
, but it does not help. They does not have all the word lists that it should return. Any help will be greatly appreciated. Thank you.
解决方案
Use itertools.product
:
>>> 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
To get word from min length to max length:
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)
这篇关于Python - 如何从特定长度的给定字符生成词表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!