-1

I got this list of list of list

A=[[[0, 'a'], [2, 'g'], [9, 'Q'], [12, 'front'], [0, 'Z'], [0, 'w'], [2, 'front'], [0, 'Z']], [[2, 'a'], [0, 'c'], [0, 'g'], [15, 'w'], [2, 'Q'], [12, 'front'], [0, 'Z'], [2, 'front'], [0, 'Z']]

and in each list of list there are lists with similar alphabets.In this example,it is 'front'.I want to remove the list with similar alphabets,keeping the one with the larger number and removing the one with a smaller number.

Output should be like:

A=[[[0, 'a'], [2, 'g'], [9, 'Q'], [12, 'front'], [0, 'Z'], [0, 'w'], [0, 'Z']], [[2, 'a'], [0, 'c'], [0, 'g'], [15, 'w'], [2, 'Q'], [12, 'front'], [0, 'Z'],[0, 'Z']]

Order doesn't matter

Sook Yee Lim
  • 101
  • 1
  • 9

1 Answers1

0

I don't really get if you want to have apply your process for each list of list separatly or not, but if you do here is one solution:

for i, list_dict in enumerate(A):
   uni_dict = {}
   for value, key in list_dict:
      if key in uni_dict:
         uni_dict[key] = max(uni_dict[key], value)
      else:
         uni_dict[key] = value

   A[i] = [[value, key] for key, value in uni_dict.iteritems()]