Python trying to randomly select the value from list within a list results in the TypeError: list expected at most 1 argument, got 4

Python trying to randomly select the value from list within a list results in the TypeError: list expected at most 1 argument, got 4

我尝试了很多搜索,但找不到合适的解决方案,因此在此处发布。 我是 Python 的新手,我正在尝试创建一个简单的密码生成器应用程序,它有 8 个字符,应该由 1 个大写字母、1 个小写字母、1 个特殊字符和 1 个数值组成。我能够得到这些东西并创建一个包含大写字母、小写字母、特殊字符和数值的 4 个字母的密码。对于剩下的 4 个字符,我想从由所有这些选项组成的列表中随机选择选项,但由于某种原因,我无法为其随机获取值。当我尝试从列表中的列表访问随机选择时出现以下错误:

TypeError: list expected at most 1 argument, got 4

我想知道如何 select 一个随机值,它可以是小写、大写、数字或特殊字符,用于我最终密码中的其余 4 个值。我知道我可以使用 for 循环来完成同样的事情,但我想随机进行,所以我正在尝试这种方法。

以下是我到目前为止的代码以及我尝试获取位于列表中的随机值列表的一些事情:

import random
import string

def passwordGenerator():
    lowerchars      =   list(string.ascii_lowercase)
    upperchars      =   list(string.ascii_uppercase)
    speciachars     =   ['&','!','_','@']
    numericchars    =   list(range(0,9))
    otherrandom     =   list(string.ascii_lowercase, string.ascii_uppercase, range(0,9), ['&','!','_','@'])
    #otherrandom     =   list(list(string.ascii_lowercase), list(string.ascii_uppercase) list(range(0,9)), ['&','!','_','@'])
    print(random.choice(otherrandom))
    #print(random.choice(random.choice(otherrandom)))
    password        = random.choice(lowerchars) + random.choice(upperchars) + random.choice(speciachars) + str(random.choice(numericchars))

passwordGenerator()

list() 方法只有一个参数。 你可以做到

otherrandom = lowerchars + upperchars + numericchars + speciachars

这会将所有列表加在一起,这可能就是您想要的。