Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python的if语句中,是否可以将一个变量与其他几个变量进行比较?_Python_Python 3.x_If Statement - Fatal编程技术网

在python的if语句中,是否可以将一个变量与其他几个变量进行比较?

在python的if语句中,是否可以将一个变量与其他几个变量进行比较?,python,python-3.x,if-statement,Python,Python 3.x,If Statement,我想改进以下if声明 #Extract ORG, GPE, LOC and FAC labels from phrases for entity in doc.ents: if entity.label_ == "ORG" or entity.label_ == "GPE" or entity.label_ == "LOC" or entity.label_ == "FAC": print(e

我想改进以下if声明

#Extract ORG, GPE, LOC and FAC labels from phrases
for entity in doc.ents:
    if entity.label_ == "ORG" or entity.label_ == "GPE" or entity.label_ == "LOC" or entity.label_ == "FAC":
        print(entity.text, entity.label_)

是否可以将“entity.label”变量的数量减少到一个?

您可以尝试检查所有单词的元组中是否存在
entity.label\ucode>var

for entity in doc.ents:
    if entity.label_ in ("ORG", "GPE", "LOC", "FAC"):
        print(entity.text, entity.label_)

如果entity.label位于('ORG','GPE','LOC','FAC'):
是的,谢谢@赫尔伍德
for entity in doc.ents:
    if any(entity.label_ == x for x in ["ORG", "GPE", "LOC", "FAC"]):
        print(entity.text, entity.label_)