我想到的一个例子是这样的:
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
: