在python的if语句中,是否可以将一个变量与其他几个变量进行比较?
我想改进以下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
#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_)