【问题标题】:Generating a 10-digit password生成 10 位密码
【发布时间】:2022-08-17 02:33:21
【问题描述】:

所以我需要生成一个10位密码(需要使用random模块),每次都必须包含2个小写字母、2个大写字母、3个特殊符号和3个数字,所有这些都以随机顺序排列。我已经完成了随机密码生成器部分,但我不确定如何将其限制为 2 个小写字母、2 个大写字母、3 个特殊符号和 3 个数字。

这是我到目前为止所拥有的:

import random
import string
lc_letter = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\",\"i\",\"j\",\"k\",\"l\",\"m\",\"n\",\"o\",\"p\",\"q\",\"r\",\"s\",\"t\",\"u\",\"v\",\"w\",\"x\",\"y\",\"z\"]
uc_letter = [\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"I\",\"J\",\"K\",\"L\",\"M\",\"N\",\"O\",\"P\",\"Q\",\"R\",\"S\",\"T\",\"U\",\"V\",\"W\",\"X\",\"Y\",\"Z\"]
symbols = [\"!\",\"@\",\"#\",\"$\",\"%\",\"^\",\"&\",\"*\",\"(\",\")\",\"_\",\"+\",\"=\",\"-\",\"/\",\">\",\"<\",\",\",\".\",\"?\",\"\\\\\"]
numbers = [\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"]
options = [lc_letter,uc_letter,symbols,numbers]
for i in range(10):
    choice = random.choice(options)
    digit = random.choice(choice)
    print(digit, end = \'\')
  • 好吧,您的要求加起来就是您需要的符号总数。那么为什么不从每个类别中选择所需的数量,然后将它们混在一起呢?

标签: python password-generator


【解决方案1】:

您可以使用来自string 的常量:

import random
import string

s = ""

for i in range(2):
    s = s + random.choice(string.ascii_lowercase)
for i in range(2):
    s = s + random.choice(string.ascii_uppercase)
for i in range(3):
    s = s + random.choice(string.punctuation)
for i in range(3):
    s = s + random.choice(string.digits)

s = ''.join(random.sample(s, 10))

print(s)

【讨论】:

    【解决方案2】:

    我建议的另一种方法是,从大写、小写等中取出 2 个字母,然后使用 random.shuffle 方法对生成的密码进行洗牌。

    【讨论】:

      【解决方案3】:

      首先选择每个需要的字符,然后将它们洗牌:

      from random import choice as rd
      from random import shuffle
      import string
      lc_letter = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
      uc_letter = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
      symbols = ["!","@","#","$","%","^","&","*","(",")","_","+","=","-","/",">","<",",",".","?","\\"]
      numbers = ["0","1","2","3","4","5","6","7","8","9"]
      options = [
          rd(lc_letter),
          rd(lc_letter),
          rd(uc_letter),
          rd(uc_letter),
          rd(symbols),
          rd(symbols),
          rd(symbols),
          rd(numbers),
          rd(numbers),
          rd(numbers),
      ]
      shuffle(options)
      print(''.join(options))
      

      【讨论】:

        【解决方案4】:

        您可以使用random.choicerandom.samplestring 模块中的常量来获取随机生成的密码。

        import random
        import string
        lc_letter = string.ascii_lowercase
        uc_letter = string.ascii_uppercase
        # Could use string.punctuation here, but it would be different
        # as your list doesn't contain semicolons or colons,
        # while string.punctuation does.
        symbols = ["!","@","#","$","%","^","&","*","(",")","_","+","=","-","/",">","<",",",".","?","\\"]
        numbers = string.digits
        
        lc_selection = [random.choice(lc_letter) for _ in range(2)]
        uc_selection = [random.choice(uc_letter) for _ in range(2)]
        symbol_selection = [random.choice(symbols) for _ in range(3)]
        number_selection = [random.choice(numbers) for _ in range(3)]
        print(''.join(random.sample(lc_selection + uc_selection + symbol_selection + number_selection, 10)))
        

        【讨论】:

          【解决方案5】:

          您实际上可以做的是制作一个长度为 10 的列表,如下所示:

          dist = [0, 0, 1, 1, 2, 2, 2, 3, 3, 3]
          

          此列表表示您的options 列表中每个索引的分布。 例如,您在选项中将小写字母放在首位,您必须选择 2 个小写值,因此分配列表中有 2 个零。

          现在您可以在列表中选择一个索引:

          idx = random.randint(0, len(dist))
          

          然后,从列表中选择您的选择:options[dist[idx]]

          最后是pop 的值idx 来自dist

          dist.pop(idx)
          

          这将以相同的概率生成所有有效密码。

          【讨论】:

            【解决方案6】:

            我认为 Yevgeniy Kosmak 解决方案的更好版本(独立配置更清晰可见,循环避免代码重复,使用 choices 而不是 choice 避免循环)。

            import random
            import string
            
            config = [
                (2, string.ascii_lowercase),
                (2, string.ascii_uppercase),
                (3, string.punctuation),  # or use your '!@#$%^&*()_+=-/><,.?\\'
                (3, string.digits),
            ]
            
            picked = []
            for k, options in config:
                picked += random.choices(options, k=k)
            random.shuffle(picked)
            password = ''.join(picked)
            
            print(password)
            

            Try it online!

            【讨论】:

              【解决方案7】:

              谁能给我 cuppy.py 文件,包括这些方法编号字母和符号

              【讨论】:

                猜你喜欢
                • 2017-07-21
                • 1970-01-01
                • 1970-01-01
                • 2011-04-03
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多