列表理解/生成器表达式中的"x in y"的反义词是什么?



我想到的一个例子是这样的:

identified_characters = ["a","c","f","h","l","o"]
word = "alcachofa#"
if any(character in word for character not in identified_characters):
print("there are unidentified characters inside the word")
else:
print("there aren't unidentified characters inside the word")

但是not带来了语法错误,所以我想,如果理论上有out(我想与in相反(函数,你可以更改not in并保留语法。

我还认为给定结果的逻辑应该与any函数相反,但抬头一看,我发现ppl得出any的相反应该是not all,在这种情况下,这里不起作用。

不能循环遍历identified_characters中以外的所有可能项目;数量多得莫名其妙。这甚至在概念上都没有意义。

为了实现您想要的(检查word中是否有未识别的字符(不在identified_characters中的字符(,您必须循环使用word,而不是identified_characters的补码。

identified_characters = {"a", "c", "f", "h", "l", "o"}
word = "alcachofa#"
if any(character not in identified_characters for character in word):
print("there are unidentified characters inside the word")
else:
print("there aren't unidentified characters inside the word")

不要在for语句中使用not,而是在character in word部分使用它。

identified_characters=["a","c","f","h","l","o"]
word="alcachofa#"
if any(character not in identified_characters for character in word):
print("there are unidentified characters inside the word")
else:
print("there aren't unidentified characters inside the word")

For循环可以使用in,但不能使用not in,因为它们不知道not in的含义!For循环意味着遍历列表或任何可迭代的,并且不能遍历不在可迭代中的内容,因为它们不知道什么是";不在";可迭代的。您也可以通过以下方式使用not all

最新更新