我想让这更简单并且能够搜索变量列表:alarm1,alarm2,alarm3 ...在alarmDict.values()
到目前为止,我的工作是:
import xml.etree.ElementTree as ET
tree = ET.parse (path.filename) #XML file
root = tree.getroot()
for child in root:
alarmDict = child.attrib #This lists the alarm codes as dicts
if 'alarm1' in alarmDict.values(): print ('Contains this alarm', 'alarm1')
if 'alarm2' in alarmDict.values(): print ('Contains this alarm', 'alarm2')
if 'alarm3' in alarmDict.values(): print ('Contains this alarm', 'alarm3')
我想要的是什么:
for child in root:
alarmDict = child.attrib #This lists the alarm codes as dicts
alarm_list = [alarm1, alarm2, alarm3]
if alarm_list in alarmDict.values(): print ('Contains this alarm', alarm_list[])
答案 0 :(得分:0)
你需要另一个循环:
for child in root:
alarmDict = child.attrib #This lists the alarm codes as dicts
alarm_list = [alarm1, alarm2, alarm3]
for alarm in alarm_list:
if alarm in alarmDict.values():
print('Contains this alarm', alarm)
如果您想深入了解检查列表内容与其他列表的最佳方式,我建议this answer