Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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,我是python新手,我想生成随机密码。为此,我创建了三个可能包含字母、数字和特殊字符的列表。我将这些列表添加到另一个名为“所有字符”的列表中 现在我想在“所有字符”中选择一个随机字母、数字或特殊字符。为此,我生成了一个随机数,用作索引,从“所有字符”中获取字母、数字或特殊字符的列表。但从现在起,我不知道选择了哪个列表,因此我不知道如何获得特定列表的长度来选择随机项。你知道如何在同一行代码中解决这个问题吗?或者我必须存储所选列表的索引,以便获得其长度 我希望执行此操作的代码行如下所示,请参见??

我是python新手,我想生成随机密码。为此,我创建了三个可能包含字母、数字和特殊字符的列表。我将这些列表添加到另一个名为“所有字符”的列表中

现在我想在“所有字符”中选择一个随机字母、数字或特殊字符。为此,我生成了一个随机数,用作索引,从“所有字符”中获取字母、数字或特殊字符的列表。但从现在起,我不知道选择了哪个列表,因此我不知道如何获得特定列表的长度来选择随机项。你知道如何在同一行代码中解决这个问题吗?或者我必须存储所选列表的索引,以便获得其长度

我希望执行此操作的代码行如下所示,请参见??:

当您输入密码时!=16: 密码+=所有字符[random.randint0,lenall_chars][random.randint0,?]

完整代码如下:

import string
import random

letters = list(string.ascii_letters)
numbers = [x for x in range(0, 10)]
special_chars = ['+', '-', '!', '&', '=', ':', '.', ',', ';', '?', '@', '#', '%']
all_chars = [letters, numbers, special_chars]
password = ""

while len(password) != 16:
    password += all_chars[random.randint(0, len(all_chars))][random.randint(0, ???)] 

print(password)

提前感谢您的帮助

您可以将其保存在变量中,以获取其长度:

while len(password) != 16:
    chars_selection = all_chars[random.randint(0, len(all_chars))]
    password += chars_selection[random.randint(0, len(chars_selection))] 
这将给你不同的概率,但你可以把所有字符放在一个唯一的列表中

from string import ascii_letters, digits

letters = list(ascii_letters)
numbers = list(digits)
special_chars = list('+-!&=:.,;?@#%')
all_chars = [*letters, *numbers, *special_chars]

while len(password) != 16:
    password += all_chars[random.randint(0, len(all_chars))]

我不建议使用random生成密码。这是不安全的。相反,尝试使用这里的秘密-

如果您要使用更推荐的模块,您可以这样生成它-

unit_list = ['+', '-', '!', '&', '=', ':', '.', ',', ';', '?', '@', '#', '%']
secure_choice = secretsGenerator.choice(unit_list)
print ("Secure random choice using secrets is ", secure_choice)

希望这有帮助

您可以要求random从序列中为您提供一个索引,而不是使用索引:

password += choice(choice(all_chars))

需要注意的是,random模块不用于生成密码。相反,您应该使用以下模块:


谢谢你的提示!事实上,这只是为了锻炼身体,但一旦真的需要,知道如何正确地做是很好的。那太棒了!很高兴我能帮上忙!
import secrets
import string


special_chars = ['+', '-', '!', '&', '=', ':', '.', ',', ';', '?', '@', '#', '%']
all_chars = string.ascii_letters + string.digits + special_chars

def gen_password(password_length=16):
    return ''.join(secrets.choice(all_chars) for i in range(password_length))