Grouping indices of unique elements in numpy

前端 未结 5 587
伪装坚强ぢ
伪装坚强ぢ 2021-01-17 17:15

I have many large (>100,000,000) lists of integers that contain many duplicates. I want to get the indices where each of the element occur. Currently I am doing something li

5条回答
  •  情歌与酒
    2021-01-17 17:55

    this can be solved via python pandas (python data analysis library) and a DataFrame.groupby call.

    Consider the following

     a = np.array([1, 2, 6, 4, 2, 3, 2])
    
     import pandas as pd
     df = pd.DataFrame({'a':a})
    
     gg = df.groupby(by=df.a)
     gg.groups
    

    output

     {1: [0], 2: [1, 4, 6], 3: [5], 4: [3], 6: [2]}
    

提交回复
热议问题