I have my res_list of string that looks like this
Sleep Morning_Meds 2
Morning_Meds Watch_TV 1
Watch_TV Sleep 3
Morning_Meds Sleep 1
Sleep Watch_TV 3
I know it's a simple task but I'm struggling doing it, what I need to achive is something like this:
Morning_Meds Watch_TV, Sleep 1
Watch_TV Sleep 3
Sleep Watch_TV 3
the logic is that I want to be left in the list the ones with higher value: for example, in the first block with my initial list there are "Sleep Morning_Meds 2" and "Sleep Watch_TV 3", in the second block it only remains "Sleep Watch_TV 3" because 3 is higher than 2. In case the value is equal to the other one like "Morning_Meds Watch_TV 1" "Morning_Meds Sleep 1" I want to create a new string including both "Watch_TV" and "Sleep".
What I tried doing is this:
for g in range(0, len(res_list) - 1):
v = res_list[g].split() # taking the first element and splitting it because it's a string
for j in range(g + 1, len(res_list)): # i'm beginning from the element next g
v1 = res_list[j].split() # also splitting it
if v[0] == v1[0]: # if the first name is equal to the next one...
if v[2] < v1[2]: # ...checking what value is higher...
res_list.remove(res_list[g]) #...and then removing the lowest of the two
elif v[2] == v1[2]: # checking if the values are equals
new_element= v[0] + " " + v[1] + ", " + v1[1] + " " + v[2]
res_list.insert(j, new_element) # adding the new string
res_list.remove(res_list[g]) # removing the old ones
res_list.remove(res_list[j])
else: # checking if the first value is higher than the next one
res_list.remove(res_list[j])
the problem here is that if I trubleshoot printing it only compare the first string "Sleep Morning_Meds 2" with "Sleep Watch_TV 3", it doesn't consider the other ones and I don't know why:
Morning_Meds Watch_TV 1
Watch_TV Sleep 3
Morning_Meds Sleep 1
Sleep Watch_TV 3
how can I do it? Thanks!