Home > Mobile >  How to compare iteratively the value of member elements of two python lists
How to compare iteratively the value of member elements of two python lists

Time:05-31

I have a continuously updating (value wise) python list called up_list.

The index of each element represent an object from a real life like Apple, Oranges, Banana, Tomato, Lemon. I want to check the value of each element (Apple or Oranges or Banana or Tomato or Lemon) in the list by comparing with its own previous version. And then, I want to trigger an action based on the comparison result if the difference between the two lists is 1.

Inserted in edit:" The objective of the code is to perform some action when one of the element of the list updates. For example: if up_list = [0,0,0,0,0] was the initial value and after one iteration up_list = [1,0,0,0,0], then the value of Apple is being updated and I can invoke some action to write the new value in my database. "

I am trying to do it like this.

up_list = [0,0,0,0,0]

loop starts here:

  up_list_new[index] = copy.copy(up_list[index])
        up_list[index] = up_list[index]   1

    if up_list[0]-up_list_new[0] == 1
       cursor.execute("INSERT INTO test (some data))

    elif up_list[1] - up_list_new[1] == 1
       cursor.execute(INSERT INTO test(some data))
   
    elif up_list[2]-up_list_new[2] == 1
       cursor.execute("INSERT INTO test (some data))

    elif up_list[4] - up_list_new[4] == 1
       cursor.execute(INSERT INTO test(some data))

My question:

  1. I am getting the value update of only a few of the list elements. Is my assumption that in each iteration of the loop updates up_list (because of up_list[index] = up_list[index] 1) so that my copying it to up_list_new is retaining the pervious version before being updated?

  2. Is there a better way to do this? imagine the complex actions to be taken after each successful ifs.

Thank you! I hope my question is clear to you guys.

Edit 2: - Here is the code from the project.

up_list = [0, 0, 0, 0, 0] up_list1 = [0, 0, 0, 0, 0]

 valuesup1 = (location, viddate, coordinates, id, 'car','up',elapsedtime2)
    valuesup2 = (location, viddate, coordinates, id, 'motorbike', 'up', elapsedtime2)
    valuesup3 = (location, viddate, coordinates, id, 'bus', 'up', elapsedtime2)
    valuesup4 = (location, viddate, coordinates, id, 'truck', 'up', elapsedtime2)
    valuesup5 = (location, viddate, coordinates, id, 'person', 'up', elapsedtime2)
    
       
    # Find the current position of the vehicle
    if (iy > up_line_position) and (iy < middle_line_position):

        if id not in temp_up_list:
            temp_up_list.append(id)
            #print('This is UP: ', up_list)

    elif iy < down_line_position and iy > middle_line_position:
        if id not in temp_down_list:
            temp_down_list.append(id)


    elif iy < up_line_position :
        if id in temp_down_list:
            temp_down_list.remove(id)

            up_list1[index] = copy.copy(up_list[index])
            up_list[index] = up_list[index]   1


            if up_list[0]-up_list1[0] == 1:
                #print('This is car - up: ')
                cursor.execute("INSERT INTO test (vidname,vidstart,coordinate,id,object,direction,elapsedtime) VALUES(%s,%s,%s,%s,%s,%s,%s)", (valuesup1))
         
            elif up_list[1] - up_list1[1] == 1:
                #print('This is motorbike - up: ')
                cursor.execute("INSERT INTO test (vidname,vidstart,coordinate,id,object,direction,elapsedtime) VALUES(%s,%s,%s,%s,%s,%s,%s)", (valuesup2))
                #conn.commit()
            elif up_list[2] - up_list1[2] == 1:
                #print('This is bus - up: ')
                cursor.execute("INSERT INTO test (vidname,vidstart,coordinate,id,object,direction,elapsedtime) VALUES(%s,%s,%s,%s,%s,%s,%s)", (valuesup3))
                #conn.commit()
            elif up_list[3] - up_list1[3] == 1:
                #print('This is truck - up: ')
                cursor.execute("INSERT INTO test (vidname,vidstart,coordinate,id,object,direction,elapsedtime) VALUES(%s,%s,%s,%s,%s,%s,%s)", (valuesup4))
                #conn.commit()
            elif up_list[4] - up_list1[4] == 1:
                #print('This is person - up: ')
                cursor.execute("INSERT INTO test (vidname,vidstart,coordinate,id,object,direction,elapsedtime) VALUES(%s,%s,%s,%s,%s,%s,%s)",(valuesup5))

            conn.commit()

CodePudding user response:

Modifying values as you're iterating over them is generally considered bad practice. I think that the easiest solution to your problem is to make a copy of original list and then you can iterate over that, for example:

up_list = [0, 0, 0, 0, 0]

# You can use [:] to create a copy of the list
up_list_new = up_list[:]

if up_list_new[0] - up_list[0] == 1:
    # Case for car
    cursor.execute(...)
         
elif up_list_new[1] - up_list[1] == 1:
    # Case for motorbike
    cursor.execute(...)

# Etc...

I think you should refactor your code to be a bit more reusable. This project will fit well with OOP, as it looks like you could make an Entry object along these lines:

class Entry:
    def __init__(self, location, viddate, ...):
        self.location = location
        self.viddate = viddate        
        # Etc.

    def save_to_db(self, table_name):
         cursor.execute(
             f"INSERT INTO {table_name} (vidname,vidstart,coordinate,id,object,direction,elapsedtime) "
               f"VALUES({self.vidname},{self.vidstart},{...},{...},{...},{...},{...}"
          )
          # Etc.

This would make you save a lot of repeated code and allow your project to be more flexible.

CodePudding user response:

It is not very clear from your question how uplist updates - but assuming you have the two versions of it -

uplist = [0, 0, 0, 0, 0]
uplist_new = [1, 0, 0, 0, 0]

# How to check if there is any change of 1 unit
any((new - old == 1) for new, old in zip(uplist_new, uplist))
# True

# How to check if there any change 
any((new != old) for new, old in zip(uplist_new, uplist))

# How to check if there is exactly one change of 1 unit
sum(new - old for new, old in zip(uplist_new, uplist)) == 1

# How to check if there is exactly one change (any number of units)
sum(((new != old) for new, old in zip(uplist_new, uplist))) == 1

  • Related