简体   繁体   English

遍历两个列表,将列表中的项目与特定值进行比较

[英]Iterate over two lists, compare items of the lists with a specific value

I perform an os.popen() command to access a measurement stored in InfluxDB from the command line.我执行os.popen()命令以从命令行访问存储在 InfluxDB 中的测量值。 The data is a table, but I am only concerned with two specific columns of the table, which is why I use splitlines() .数据是一个表,但我只关心表的两个特定列,这就是我使用splitlines()的原因。 To display the specific two columns in the GUI, I use a for loop, and I strip the title line, store values of column 2 and column 3 in separate arrays as follows:为了在 GUI 中显示特定的两列,我使用了一个for循环,并剥离了标题行,将第 2 列和第 3 列的值存储在单独的 arrays 中,如下所示:

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)

The print statements yield the output:打印语句产生 output:

['claimed', '-------', 'false', 'true']
['eya_cam', '-------', '2', '1']

I now need to perform certain if conditions:我现在需要执行某些 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

The issue with this is that when camera number '2' is given, it executes the first condition even though it has been assigned as 'false' in the Database.这样做的问题是,当给定摄像机编号“2”时,它会执行第一个条件,即使它已在数据库中指定为“假”。 Where am I going wrong with this logic?这个逻辑我哪里错了?

t in list_of_assigned
['claimed', '-------', 'false', 'true']

You are testing if the value 'true' is in your list_of_assigned variable.您正在测试值'true'是否在您的list_of_assigned变量中。 As long as there is one true in this list, t in list_of_assigned will always return true.只要此列表中有一个为真t in list_of_assigned将始终返回真。 You should either zip these two tables and parse through them together in a loop or check the index of the camera in your list_of_number list then check if that index in list_of_assigned is true.您应该 zip 这两个表并在循环中一起解析它们,或者检查list_of_number列表中相机的索引,然后检查list_of_assigned中的索引是否为真。

Did you accidentally typed assigned word?您是否不小心输入了assigned的单词?

if (camNum in list_of_number and t in list_of_assigned):

I think you may intent to write as above.我想你可能打算像上面那样写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM