list comprehension/generator 表达式中 `x in y` 的反义词是什么?

What's the opposite of `x in y` in a list comprehension/generator expression?

我举的一个例子是这样的:

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, 在这种情况下在这里不起作用。

不要在 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 循环旨在遍历列表或任何可迭代对象,并且不能遍历不在可迭代对象中的内容,因为它们不知道 what “不在”可迭代对象中。您还可以通过以下方式使用 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")