In python, I need to parse a list consisting of sublists. If the first elements of some sublists are the same, I need to pick the sublist with the least 4th element; but if the 4th elements are also the same, then I need select the sublist with higher 3rd element. For example, in the following list, I need to select sublists 1, 4 and 5.
alignments=[["A","B","10","4"],["A","C","15","8"],["A","E","20","10"],\
["D","C","15","3"],\
["G","U","1","9"],["G","O","10","9"]]
I achieved it with the code below which is very cumbersome:
best_alignments=[]
best_al=alignments[0]
k=0
c=0
counter_list=[]
for al in alignments[1:]:
c+=1
if best_al[0]==al[0]:
if best_al[3]==al[3]:
if best_al[2]<al[2]:
best_al=al
counter_list.append(c-1)
else:
counter_list.append(c)
else:
counter_list.append(c)
else:
if k==0:
best_al=al
k+=1
else:
best_al=al
for index in sorted(counter_list, reverse=True):
del alignments[index]
for el in alignments:
print(el)
I am sure there a much easier way to do that. Any suggestions are appreciated.