所有可能的8个符号字符串的生成器。暴力8符号密码。python
我需要编写生成所有可能的8个符号字符串的生成器。 从这样的符号数组:所有可能的8个符号字符串的生成器。暴力8符号密码。python,python,brute-force,Python,Brute Force,我需要编写生成所有可能的8个符号字符串的生成器。 从这样的符号数组: leters = ['1','2','3','4','5','6','7','8','9','0','q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m'] 骨架如下所示: def generator(): """ here algorithm """
leters = ['1','2','3','4','5','6','7','8','9','0','q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m']
骨架如下所示:
def generator():
"""
here algorithm
"""
yield string
假设返回如下列表['00000001','00000002','00000003',…'mmmmmmmm']
itertools.product(leters, repeat=8)
编辑:要使其提供字符串而不是元组,请执行以下操作:
def generator(leters):
a = itertools.product(leters,repeat=3)
while a:
yield "".join(a.next())
顺便说一句,字母有两个T。
itertools.combinations()
和itertools.combinations\u with\u replacement()
返回一个生成器
>>> letters = ['a', 'b', 'c']
>>> from itertools import combinations
我在示例中使用print()
来说明输出。将其替换为产量
,以获得发电机
>>> for c in combinations(letters, 2):
print(c)
...
('a', 'b')
('a', 'c')
('b', 'c')
>>> for c in combinations(letters, 2):
print(''.join(c))
...
ab
ac
bc
>>>
>>> for c in itertools.combinations_with_replacement(letters, 2):
print(''.join(c))
...
aa
ab
ac
bb
bc
cc
如果你对所有包含英文字母和数字的8个字母的密码强制使用它,那么你需要迭代2.8万亿个字符串
编辑
如果您知道没有重复的元素,请使用排列
>>> for c in itertools.permutations(letters, 2):
print(''.join(c))
...
ab
ac
ba
bc
ca
cb
这给了你ab和ba
对于最通用的暴力序列,请使用
itertools.product()
作为Cosmologicon的解决方案我也想知道如何做到这一点,这就是我想到的,我尝试了几种方法,但当我这样写的时候,速度比其他方法快得多…如果我看不到,请告诉我
import string
from itertools import permutations
[print(*p,sep='')for p in permutations(list(string.ascii_letters+string.punctuation+string.digits),8)]
这只是一个使用生成器的建议,因为您将处理超过2万亿个元素<代码>itertools.permutations('abcdefgh…',8)permutations不会给出包含重复元素的结果,例如,任何包含两个或多个0的密码。这只会提供按字典顺序排列的单词,不是吗?我在列表中没有看到“ba”。这需要多少时间才能完成?@retrocode len(leters)**8*处理一个组合所需的时间。但我确实看到是否需要其中任何一个重复组合?这是因为输出的字符本身不在字符串中。
import string
from itertools import permutations
[print(*p,sep='')for p in permutations(list(string.ascii_letters+string.punctuation+string.digits),8)]