生成所有符合条件的密码

时间:2019-10-09 20:13:49

标签: python generator

我正在尝试创建一个程序,该程序将生成包含2个小写字母,2个数字和3个大写字母的所有密码。我一直在尝试使用Streams在Java中进行此操作。当我看到自己无处可去时,我决定用Python来做(自从我刚开始使用它以来,我并没有什么知识) 最初,我认为我将使用itertools.combinations和3个列出所有数字,大写字母和小写字母的列表,但我的内存不足(32GB或RAM)。然后,我尝试在自己的变量中获取数字,小写字母和大写字母的所有组合的列表。现在,我不知道如何将它们放在一起。

这是我目前拥有的

from itertools import combinations_with_replacement
from string import ascii_lowercase
from string import ascii_uppercase

digits = '1234567890'
lowLetters = ascii_lowercase
upLetters = ascii_uppercase

digitComb = combinations_with_replacement(digits, 2)
upLettersComb = combinations_with_replacement(upLetters, 2)
lowLettersComb = combinations_with_replacement(lowLetters, 3)
fullList = digitComb + upLettersComb + lowLettersComb

我真的不知道从这里去哪里。

2 个答案:

答案 0 :(得分:0)

您熟悉正则表达式吗?如果是这样,您可以(使用某些库)生成正则表达式的所有可能匹配项:

^(?=.{7}$)(?=[^0-9]*[0-9][^0-9]*[0-9][^0-9]*)(?=[^A-Z]*[A-Z][^A-Z]*[A-Z][^A-Z]*[A-Z][^A-Z]*)(?=[^a-z]*[a-z][^a-z]*[a-z][^a-z]*).*$

就像Nico238所说的那样,您的记忆虽然不喜欢它。

答案 1 :(得分:0)

好吧,我设法创建了生成密码的脚本。另外,我将做一个更正,指出我不需要为7的每个排列生成所有可能性。相反,它必须成组完成。

from itertools import product
import string

numbers = string.digits
uppercaseList = string.ascii_uppercase
lowercaseList = string.ascii_lowercase

number_prod = product(numbers, numbers)
up_prod = product(uppercaseList, uppercaseList)
low_prod = product(lowercaseList, lowercaseList, lowercaseList)

result = product(number_prod, up_prod, low_prod)

f = open("passwords.txt", "w+")
print("Please wait")
for i in result:
    f.write(''.join(str(x) for v in i for x in v) + "\n")
f.close()
print("done")


相关问题