有没有更好的方法来避免 if 语句多次重复条件?
for entity in entities:
if (entity.entity_id.startswith('sensor') and "sourcenodeid" not in entity.entity_id and "interval" not in entity.entity_id and "previous" not in entity.entity_id and "exporting" not in entity.entity_id and "management" not in entity.entity_id and "yr" not in entity.entity_id and "alarm" not in entity.entity_id ):
data = remote.get_state(api, entity.entity_id)
#print(data)
我试过了,or
但它不能正常工作,因为我得到了不应该存储在数据中的条件的实体。
使用带有all
.
if entity.entity_id.startswith('sensor') and all(elem not in entity.entity_id for elem in ("sourcenodeid", "interval", "previous", "exporting", "management", "yr", "alarm")):
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句