How to get a list of all indices of repeated elements in a numpy array?

后端 未结 6 759
你的背包
你的背包 2020-12-03 03:37

I\'m trying to get the index of all repeated elements in a numpy array, but the solution I found for the moment is REALLY inefficient for a large (>20000 elements) input

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 04:31

    so I was unable to get rid of the for loop, but I was able to pair it down to using the for loop marginally using the set data type and the list.count() method:

    data = [1,2,3,1,4,5,2,2]
    indivs = set(data)
    
    multi_index = lambda lst, val: [i for i, x in enumerate(lst) if x == val]
    
    if data != list(indivs):
        dupes = [multi_index(data, i) for i in indivs if data.count(i) > 1]
    

    Where you loop over your indivs set, which contains the values (no duplicates) and then loop over the full list if you find an item with a duplicate. Am looking into numpy alternative if this isn't fast enough for you. Generator objects might also speed this up if need be.

    Edit: gg349's answer holds the numpy solution I was working on!

提交回复
热议问题