Efficient way to count unique elements in array in numpy/scipy in Python

后端 未结 4 869
傲寒
傲寒 2021-02-02 15:16

I have a scipy array, e.g.

a = array([[0, 0, 1], [1, 1, 1], [1, 1, 1], [1, 0, 1]])

I want to count the number of occurrences of each unique ele

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-02 15:24

    for python 2.6 <

    import itertools
    
    data_array = [[0, 0, 1], [1, 1, 1], [1, 1, 1], [1, 0, 1]]
    
    dict_ = {}
    
    for list_, count in itertools.groupby(data_array):
        dict_.update({tuple(list_), len(list(count))})
    

提交回复
热议问题