【发布时间】:2022-06-14 09:23:09
【问题描述】:
我正在编写一个 python 代码来查找密码与特定规则的所有可能组合
- 应包含字母 A-Z a-z
- 应包含数字 0-9
- 应包含特殊符号
- 密码的第一个字符必须是大写字母
from itertools import permutations
pw = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[@_!#$%^&*()<>?/\|}{~:]"
firstchar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
c = permutations(pw, 2) #3 is the password length for providing sample output quickly
f=open("password.txt","w+")
f.truncate(0)
for x in firstchar:
for i in c:
current_pw = x + "".join(i)
f.write( "\t" + current_pw + "\n" )
** 输出仅包含从 A 开始的密码,并且停止不针对 B、C 进行迭代... **
【问题讨论】:
-
请注意,
itertools.permutations返回一个iterator,而不是列表或其他序列类型。迭代器只能使用一次。
标签: python-3.x for-loop nested-loops