Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to group indices of the same elements in a list

Let's say I have a list that looks like:

[1, 2, 2, 5, 8, 3, 3, 9, 0, 1]

Now I want to group the indices of the same elements, so the result should look like:

[[0, 9], [1, 2], [3], [4], [5, 6], [7], [8]]

How do I do this in an efficient way? I try to avoid using loops so any implementations using numpy/pandas functions are great.

like image 293
Steven Chan Avatar asked Dec 14 '22 11:12

Steven Chan


2 Answers

Using pandas GroupBy.apply, this is pretty straightforward—use your data to group on a Series of indices. A nice bonus here is you get to keep the order of your indices.

data = [1, 2, 2, 5, 8, 3, 3, 9, 0, 1]
pd.Series(range(len(data))).groupby(data, sort=False).apply(list).tolist()
# [[0, 9], [1, 2], [3], [4], [5, 6], [7], [8]]
like image 140
cs95 Avatar answered Feb 16 '23 00:02

cs95


You can use a collections.defaultdict to group indices:

from collections import defaultdict

lst = [1, 2, 2, 5, 8, 3, 3, 9, 0, 1]

d = defaultdict(list)
for i, x in enumerate(lst):
    d[x].append(i)

print(list(d.values()))
# [[0, 9], [1, 2], [3], [4], [5, 6], [7], [8]]

Which also maintains order of indices added without sorting.

like image 21
RoadRunner Avatar answered Feb 15 '23 23:02

RoadRunner