Python - 如果x不在a,b,c中的任何一个

mon*_*ole 2 python if-statement

在我正在编写的程序中,我需要执行以下操作:

if (x not in a) and (x not in b) and (x not in c):
Run Code Online (Sandbox Code Playgroud)

当然,这非常乏味,特别是当a,b和c都有更长的名字时.

是否有内置函数可以执行此操作:

if x is_in_one_of(a, b, c):
Run Code Online (Sandbox Code Playgroud)

我知道如何使用函数执行此操作,我只是想知道是否有内置方法来执行此操作.

提前致谢

ssh*_*124 12

你可以这样做:

if all(x not in i for i in (a,b,c)):
Run Code Online (Sandbox Code Playgroud)

在all上述将仅评估为True如果x不是以任何的a,b,或c的

或者换句话说:

if not any(x in i for i in (a,b,c)):
Run Code Online (Sandbox Code Playgroud)

  • 恕我直言'不是任何一个(我在(a,b,c)中的i中的x)更直观. (5认同)