【问题标题】:Why I am not able to Iterate my first FOR loop为什么我无法迭代我的第一个 FOR 循环
【发布时间】:2022-06-14 09:23:09
【问题描述】:

我正在编写一个 python 代码来查找密码与特定规则的所有可能组合

  1. 应包含字母 A-Z a-z
  2. 应包含数字 0-9
  3. 应包含特殊符号
  4. 密码的第一个字符必须是大写字母
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


【解决方案1】:

我认为permutation 在第一个循环后退出。 所以你在每个循环中定义c

from itertools import permutations

pw = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[@_!#$%^&*()<>?/\|}{~:]"
firstchar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

f=open("password.txt","w+")
f.truncate(0)

for x in firstchar:
    c = permutations(pw, 2)          # c is defined here
    for i in c: 
        current_pw = x + "".join(i)
        f.write( "\t" + current_pw + "\n" )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多