Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
写这篇文章的更好方法;“双筒列表”;用Python_Python - Fatal编程技术网

写这篇文章的更好方法;“双筒列表”;用Python

写这篇文章的更好方法;“双筒列表”;用Python,python,Python,我的意思是,第12行的for循环和嵌套在其中的for循环。我不止一次遇到过这样的情况。我会使用列表理解,但在这里似乎不起作用 import random import string def password_generator(): key = zip(string.digits, string.ascii_uppercase) cruft, x = str(random.random()).split('.') pw = '' for item in

我的意思是,第12行的for循环和嵌套在其中的for循环。我不止一次遇到过这样的情况。我会使用列表理解,但在这里似乎不起作用

import random

import string

def password_generator():
    key = zip(string.digits, string.ascii_uppercase)

    cruft, x = str(random.random()).split('.')

    pw = ''

    for item in x:
        for element in key:
            if item in element:
                Q = random.random()
                if Q > 0.7:
                    pw += element[1].lower()
                else:
                    pw += element[1]

    print pw

谢谢。

这里有一种使用列表理解的方法:

def pw_gen():
    key = zip(string.digits, string.ascii_uppercase)

    cruft, x = str(random.random()).split('.')

    def f(i,e):
      Q = random.random()
      if Q > 0.7:
        return e[1].lower()
      else:
        return e[1]

    return [ f(item,element) for item in x for element in key if item in element ]

这将返回一个字符列表。使用
“”。join(pw_gen())
将其转换为字符串。

我认为对您来说很难做到这一点的是,您已经获得了随机。random()您可以使用它来组合这两种方法,但我感觉可以使用来自random的更高级别的方法简化此任务

symbols=string.ascii_uppercase[:10]
pw = ''
for i in range(15):
  letter = random.choice(symbols)
  if random.random() > 0.7:
    letter = letter.lower()
  pw += letter
甚至更短:

symbols = (7*string.ascii_uppercase[:10] +
           3*string.ascii_lowercase[:10])
pw = ''.join(random.choice(symbols) for i in range(15))
您构建
key
的方式使其成为一个非常低效的查找表。您可以替换几行以使用更有效的方法:

key = dict(zip(string.digits, string.ascii_uppercase))
#...
for item in x:
    letter = key[item]

诚然,这些都会产生一个固定长度的密码,而字符串转换很可能会得到一个更短的数字。不过,出于类似的原因,最后一个数字的随机性要比其他数字小

与使用列表理解替换内部for循环相比,您的逻辑可以简化很多。这将相当于您的代码:

import random
import string
key = dict(zip(string.digits, string.ascii_uppercase))
cruft, x = str(random.random()).split('.', 1)
pw = ''
for item in x:
    if item in key: # This if is not required as all the single digits are present
        Q = random.random()
        if Q>0.7:
            pw += key[item].lower()
        else:
            pw += key[item]
print pw
但是,如果您真的希望密码是唯一的,并且不需要轻松地进行反向工程,我建议您使用。如果您想要一个更好的密码生成器(带有字母数字和符号),您可以执行以下操作

请注意,它不是一个官方的python站点,并且可以在许多方面进行改进。像往常一样,我的意思是,这将是你的决定,以任何方式使用它。

这(随机选择的
random.choice
example)是一种非常好的(更像pythonic)方法。Python哲学的一部分是“应该有一种——最好只有一种——显而易见的方法来实现”,这在很大程度上依赖于“包括电池”(并使用电池!!!)
import random
import string
key = dict(zip(string.digits, string.ascii_uppercase))
cruft, x = str(random.random()).split('.', 1)
pw = ''
for item in x:
    if item in key: # This if is not required as all the single digits are present
        Q = random.random()
        if Q>0.7:
            pw += key[item].lower()
        else:
            pw += key[item]
print pw