这是我的Python代码:
# 1)
if (x not in z for z in y):
# 2)
if (x not in y):
其中y可能是一个列表,例如:
y = ['1','2','3']
有人能解释一下这两个句子之间的区别吗? 谢谢!
# 1)
if (x not in z for z in y):
# 2)
if (x not in y):
其中y可能是一个列表,例如:
y = ['1','2','3']
有人能解释一下这两个句子之间的区别吗? 谢谢!
首先,(x not in z for z in y)
是一个生成器语句,如果在其前面添加 if
,它将始终返回 True。
if (x not in z for z in y): # Always returns True
x
。
例如:if any(x in z for z in y): # Returns True if any of the z's contain x
if all(x in z for z in y): # Returns True only if all of the z's contain x
如果y
是这样的:
y = ['hello','how','are','you']
那么,如果x
是'e'
,则上面的any
推导式将返回True
,但all
推导式将返回False
。
因此,在生成器推导式中发生了什么:如果y是一个列表,并且您进行测试:
(x not in z for z in y)
y = [1, 2, 3]
and
if (x not in z for z in y):
if (x not in y):
如果你能测试实际列表中的成员资格,那么将成功。
当你拥有
y = ['1','2','3']
y = [(1,), (2,), (3,)]
(x not in z for z in y)
首先从简单的开始:
if (x not in y):
这里的括号没有任何含义,因此这等同于:
if x not in y:
str.replace()
方法用于将一个字符串中的一些文本替换为另一些文本。在这个方法中,你需要指定要被替换的原始文本(例如 'world'
)和新的文本(例如 'Python'
),并将它们作为字符串参数传递给该方法。例如,如果你有一个字符串 x = "Hello, world!"
,那么 x.replace('world', 'Python')
将返回一个新的字符串 "Hello, Python!"
,其中所有出现的 'world'
都被替换为 'Python'
。注意,这个方法不会改变原始字符串,而是返回一个新的替换后的字符串。if (x not in z for z in y)
这里我们有一个生成器表达式。生成器表达式的格式为(x for z in y)
,等同于以下代码:
for z in y:
yield x
你可能听说过 列表解析;它们与生成器表达式类似,但使用方括号而不是圆括号: [x for z in y]
。由于它们返回一个列表,因此更容易理解。它们等同于以下内容:
lst = []
for z in y:
lst.append(x)
return lst
y
的元素,在每个迭代中调用元素z
并返回该元素的x
。在您的情况下,x
本身就是一个表达式:x not in z
,这基本上与上面的相同:您正在检查x
是否不包含在z
中。
现在,生成器表达式有点复杂,因为当请求元素时它们被评估,所以现在假设我们有一个列表推导式。
if [x not in z for z in y]:
这个功能是为每个在列表 y
中的元素 z
计算 x not in z
。所以对于你的 y
,结果列表将会是这样的:
[x not in '1', x not in '2', x not in '3']
x
,这将导致一个包含三个布尔值的列表。现在,非空列表总是会被视为 trueish,因此无论检查结果如何,if
检查都将成功。True
。为此,我们可以使用 all()
函数,它基本上检查列表或生成器中是否仅包含 true 值。if all(x not in z for z in y):
如果x
不包含在列表y
的任何元素中,那么这将成功。如果另一方面我们想要检查列表或生成器中是否至少有一个真值,那么我们可以使用any()
函数。
This
if (x not in z for z in y):
is equivalent to
if True:
Because
>>> (x not in z for z in y)
<generator object <genexpr> at 0x000000E7AE26FB88>
>>> bool(_)
True
This
if (x not in y):
Is the same as
if x not in y:
Which resolves internally to
if not y.__contains__(x):
if any(x not in z for z in y):
# same as
if not all(x in z for z in y):
或者:
if all(x not in z for z in y):
# same as
if not any(x in z for z in y):
1) if (x not in z for z in y):
< p > for
in语句表示您有一个“列表推导式”或“生成器表达式”(在括号内)。它将生成一个列表。这是一个带有过滤器的列表推导式(x not in z
部分)。但最终结果将是一个可能为空的列表。现在,if
语句将对其进行“真值”评估,其中空列表被视为false,非空列表则为true。
2) if (x not in y):
这是一个直接的“包含测试”,用于测试可迭代对象y
中是否直接包含(或不包含)变量x
,并返回一个布尔值。根据y
的类型,这可能是一个快速测试。
第一种形式在大多数情况下可能更慢且不必要。第一种形式必须创建然后销毁一个临时列表。
x = 'h' > True
;x = 'p' > False
;x = 'hello' > False
。 - Samie Bencherif