Python 迭代两个列表,将列表中的项目与特定值进行比较
我执行一个Python 迭代两个列表,将列表中的项目与特定值进行比较,python,python-3.x,list,Python,Python 3.x,List,我执行一个os.popen()命令,从命令行访问InfluxDB中存储的测量值。数据是一个表,但我只关心表的两个特定列,这就是为什么我使用splitlines()。 为了在GUI中显示特定的两列,我使用for循环,并剥离标题行,将第2列和第3列的值存储在单独的数组中,如下所示: list_of_number = [] list_of_assigned = [] for line in output[1:]: self.cameraOutputTextEdit.append(line[2]
os.popen()
命令,从命令行访问InfluxDB中存储的测量值。数据是一个表,但我只关心表的两个特定列,这就是为什么我使用splitlines()
。
为了在GUI中显示特定的两列,我使用for
循环,并剥离标题行,将第2列和第3列的值存储在单独的数组中,如下所示:
list_of_number = []
list_of_assigned = []
for line in output[1:]:
self.cameraOutputTextEdit.append(line[2] + " " + line[1])
dict = {}
dict['claimed'] = line[1]
dict['eya_cam'] = line[2]
list_of_assigned.append(dict['claimed'])
list_of_number.append(dict['eya_cam'])
print(list_of_assigned)
print (list_of_number)
print语句产生以下输出:
['claimed', '-------', 'false', 'true']
['eya_cam', '-------', '2', '1']
我现在需要执行某些if条件:
camNum = self.cameraNumber.text()
t="true"
f="false"
if (camNum in list_of_number and t in list_of assigned):
do_something
if (camNum list_of_number and f in list_of assigned):
do_something
if (camNum not in list_of_number):
do_something
问题在于,当给定摄像机编号“2”时,它会执行第一个条件,即使它在数据库中被指定为“false”。
我的逻辑哪里出了问题?您是否意外键入了
assigned
word
if (camNum in list_of_number and t in list_of_assigned):
我想你可能打算写如上所述
t in list_of_assigned
['claimed', '-------', 'false', 'true']
您正在测试值
'true'
是否在分配的变量列表中。只要此列表中有一个true,分配的列表中的t将始终返回true。您应该将这两个表压缩并在循环中一起解析,或者检查列表中的摄像机索引,然后检查列表中的索引是否为真。我尝试了查找摄像机编号索引的方法,然后在第二个列表中检查是否为真/假相同的索引位置。非常感谢你!