Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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/4/algorithm/12.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_Algorithm - Fatal编程技术网

如何从Python中的迭代列表理解中排除某个值?

如何从Python中的迭代列表理解中排除某个值?,python,algorithm,Python,Algorithm,我试图从迭代列表理解中排除某个值(特别是空格“”),但我不确定如何在列表理解中做到这一点 我正在构建一个简单的密码生成器,我知道以后如何通过执行if-else语句来实现它,但我不太确定在passcharacters.append(c)部分如何实现。我想让它得到所有的ASCI字符除了一个空格 import random as rd passlength = int(input('How long should the password be?:\n')) passcharacters = []

我试图从迭代列表理解中排除某个值(特别是空格“”),但我不确定如何在列表理解中做到这一点

我正在构建一个简单的密码生成器,我知道以后如何通过执行if-else语句来实现它,但我不太确定在passcharacters.append(c)部分如何实现。我想让它得到所有的ASCI字符除了一个空格

import random as rd

passlength = int(input('How long should the password be?:\n'))

passcharacters = []

for c in (chr(i) for i in range(32,127)):
    passcharacters.append(c)
print(passcharacters)

secure_random = rd.SystemRandom()

password = ""
for x in range(passlength):
    character = secure_random.choice(passcharacters)
    password = password + character

print("\nYour password is: \n"+password)

你应该做一个直接的列表理解,而不是附加。列表理解和生成器表达式都支持条件:

passcharacters = [chr(i) for i in range(32,127) if chr(i) != ' ']
另一种更具可读性的方式是:

import string

passcharacters = list(string.printable.strip())
你想要的是相同的字符,只是顺序不同而已。您可以通过以下方式确认这一点:

set(string.printable.strip()) == {chr(i) for i in range(32,127) if chr(i) != ' '}

这是
正确的

您应该直接理解列表,而不是附加。列表理解和生成器表达式都支持条件:

passcharacters = [chr(i) for i in range(32,127) if chr(i) != ' ']
另一种更具可读性的方式是:

import string

passcharacters = list(string.printable.strip())
你想要的是相同的字符,只是顺序不同而已。您可以通过以下方式确认这一点:

set(string.printable.strip()) == {chr(i) for i in range(32,127) if chr(i) != ' '}

这是
True

uhm。。我看到一个生成器表达式,它被迭代并附加到列表中。从字面上说,这应该只是一个列表理解开始。此外,您还可以将
if
语句放入列表理解/生成器表达式中。要排除空间,只需使用
range(33127)
:)uhm。。我看到一个生成器表达式,它被迭代并附加到列表中。从字面上说,这应该只是一个列表理解开始。此外,您还可以将
if
语句放入列表理解/生成器表达式中。要排除空间,只需使用
range(33127)
:)谢谢!超级有用:D。。。我忘了!有趣的是,你知道random.randomchoice和random.SystemRandom的区别是什么吗?@coconido请避免问不同的问题;根据需要再贴一张。无论如何,
choice
是Python中的,而
SystemRandom
是系统RNG。还要考虑<代码>秘密>代码>模块,如果你想要密码强大的RNG,请使用集合理解而不是列表理解。set@Jean-Françoisfare编辑过,不过在这个场景中您不会使用该代码。这只是为了再次确认两者是否相同。谢谢!超级有用:D。。。我忘了!有趣的是,你知道random.randomchoice和random.SystemRandom的区别是什么吗?@coconido请避免问不同的问题;根据需要再贴一张。无论如何,
choice
是Python中的,而
SystemRandom
是系统RNG。还要考虑<代码>秘密>代码>模块,如果你想要密码强大的RNG,请使用集合理解而不是列表理解。set@Jean-Françoisfare编辑过,不过在这个场景中您不会使用该代码。这只是为了再次确认两者是否相同。