简体   繁体   中英

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. The data is a table, but I am only concerned with two specific columns of the table, which is why I use 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:

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:

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

I now need to perform certain if conditions:

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. 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. As long as there is one true in this list, t in list_of_assigned will always return true. 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.

Did you accidentally typed assigned word?

if (camNum in list_of_number and t in list_of_assigned):

I think you may intent to write as above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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