count occurrences of arrays in multidimensional arrays in python

后端 未结 5 1703
醉酒成梦
醉酒成梦 2020-12-20 18:11

I have the following type of arrays:

a = array([[1,1,1],
           [1,1,1],
           [1,1,1],
           [2,2,2],
           [2,2,2],
           [2,2,2],
         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-20 18:50

    If you don't mind mapping to tuples just to get the count you can use a Counter dict which runs in 28.5 µs on my machine using python3 which is well below your threshold:

    In [5]: timeit Counter(map(tuple, a))
    10000 loops, best of 3: 28.5 µs per loop
    
    In [6]: c = Counter(map(tuple, a))
    
    In [7]: c
    Out[7]: Counter({(2, 2, 2): 3, (1, 1, 1): 3, (3, 3, 0): 3})
    

提交回复
热议问题