Home > Net >  Python list: Group by most often seen
Python list: Group by most often seen

Time:09-04

I have a 2d list [index, value] with repeated indexes. I need to select unique indexes with most often occurring value or if values seen equal number of times - the last one.

[
  [0,-1],
  [1, 0],
  [1, 1],
  [2, 1],
  [2,-1],
  [2, 1],
]
 =>
[
  [0,-1],
  [1, 1], # last seen
  [2, 1], # most often seen
]

I can use numpy or any other popular lib instead if it makes it easier

CodePudding user response:

You can do like this,

from itertools import groupby

result = []
for index, lst in groupby(l, key=lambda x:x[0]):
     lst = [i[1] for i in lst]
     if len(lst) == len(set(lst)):
         item = lst[-1]
     else:
         item = max(set(lst), key=lst.count)
     result.append([index, item])

In [160]: result
Out[160]: [[0, -1], [1, 1], [2, 1]]

len(lst) == len(set(lst)) -> Idenity if the list doesn't have any replication.

CodePudding user response:

from collections import defaultdict

input = [
  [0,-1],
  [1, 0],
  [1, 1],
  [2, 1],
  [2,-1],
  [2, 1],
]

out = []
counter_map = defaultdict(lambda: defaultdict(int))

for index, value in input:
    counter_map[index][value]  = 1
    
for index, value_count_maps in counter_map.items():
    best = None
    for value, count in value_count_maps.items():
        if best == None or value >= best[0]:
            best = [value, count]
    out.append(best)
        
print(out) # [[0, -1], [1, 1], [2, -1]]

CodePudding user response:

long_list=[[0,-1],[0,-1],[1, 0],[1, 1],[2, 1],[2,-1],[2, 1],]
short_list=[]
for element in long_list:
    if element not in short_list:
        short_list.append(element)
print(short_list)

Output: [[0, -1], [1, 0], [1, 1], [2, 1], [2, -1]]

  • Related