Count the duplicates in a list of tuples

后端 未结 5 1578
时光取名叫无心
时光取名叫无心 2021-01-18 15:45

I have a list of tuples: a = [(1,2),(1,4),(1,2),(6,7),(2,9)] I want to check if one of the individual elements of each tuple matches the same position/element i

5条回答
  •  灰色年华
    2021-01-18 16:12

    You can make count_map, and store the count of each tuple as the value.

    >>> count_map = {}
    >>> for t in a:
    ...     count_map[t] = count_map.get(t, 0)  +1
    ... 
    >>> count_map
    {(1, 2): 2, (6, 7): 1, (2, 9): 1, (1, 4): 1}
    

提交回复
热议问题