How to reduce on a list of tuples in python

后端 未结 4 1599
刺人心
刺人心 2021-01-14 09:50

I have an array and I want to count the occurrence of each item in the array.

I have managed to use a map function to produce a list of tuples.

def         


        
4条回答
  •  日久生厌
    2021-01-14 10:37

    You could use Counter:

    from collections import Counter
    arr = [11817685, 2014036792, 2014047115, 11817685]
    counter = Counter(arr)
    print zip(counter.keys(), counter.values())
    

    EDIT:

    As pointed by @ShadowRanger Counter has items() method:

    from collections import Counter
    arr = [11817685, 2014036792, 2014047115, 11817685]
    print Counter(arr).items()
    

提交回复
热议问题