Abhi
Abhi

Reputation: 1255

How to find common elements inside a list

I have a list l1 that looks like [1,2,1,0,1,1,0,3..]. I want to find, for each element the indexes of elements which have same value as the element.

For eg, for the first value in the list, 1, it should list out all indexes where 1 is present in the list and it should repeat same for every element in the list. I can wrote a function to do that iterating through the list but wanted to check if there is any predefined function.

I am getting the list from Pandas dataframe columns, it would be good know if series/dataframe library offer any such functions

Upvotes: 0

Views: 295

Answers (4)

Joe T. Boka
Joe T. Boka

Reputation: 6583

You can also try something like this:

import pandas as pd
df = pd.DataFrame({'A': [1,2,1,0,1,1,0,3]})
uni = df['A'].unique()
for i in uni:
    lists = df[df['A'] == i].index.tolist()
    print(i, '-->', lists)

Output:

1 --> [0, 2, 4, 5]
2 --> [1]
0 --> [3, 6]
3 --> [7]

Upvotes: 0

Andrew Guy
Andrew Guy

Reputation: 9968

You could achieve this using a defaultdict.

from collection import defaultdict

input = [1,2,1,0,1,1,0,3]
#Dictionary to store our indices for each value
index_dict = defaultdict(list)
#Store index for each item
for i, item in enumerate(input):
    index_dict[item].append(i)

If you want a list which contains the indices of elements which are the same as the corresponding element in your input list, you can just create a reference to the dictionary:

same_element_indices = [index_dict[x] for x in input]

This has the advantage of only referencing the one object for each identical element.

Output would be:

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

Upvotes: 0

Rehan Shikkalgar
Rehan Shikkalgar

Reputation: 1047

i tried brute force..may be u can optimize

here is python3 code

L = [1,2,1,0,1,1,0,3]
D = dict()
for i in range(len(L)):
    n =[]
    if L[i] not in D.keys():
        for j in range(len(L)):
            if L[i] == L[j]:
                n.append(j)
        D[L[i]] = n
for j in D.keys():
    print(j,"->",D.get(j))  

Upvotes: 1

Jan Christoph Terasa
Jan Christoph Terasa

Reputation: 5935

You can use numpy.unique, which can return the inverse too. This can be used to reconstruct the indices using numpy.where:

In [49]: a = [1,2,1,0,1,1,0,3,8,10,6,7]

In [50]: uniq, inv = numpy.unique(a, return_inverse=True)

In [51]: r = [(uniq[i], numpy.where(inv == i)[0]) for i in range(uniq.size)]

In [52]: print(r)
[(0, array([3, 6])), (1, array([0, 2, 4, 5])), (2, array([1])), (3, array([7])), (6, array([10])), (7, array([11])), (8, array([8])), (10, array([9]))]   

Upvotes: 1

Related Questions