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:
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 ofup_list[index] = up_list[index] + 1
) so that my copying it toup_list_new
is retaining the pervious version before being updated?Is there a better way to do this? imagine the complex actions to be taken after each successful
if
s.
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()