Count the duplicates in a list of tuples

后端 未结 5 1563
时光取名叫无心
时光取名叫无心 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:06

    Using pandas this is simple and very fast:

    import pandas
    print(pandas.Series(data=[(1,2),(1,4),(1,2),(6,7),(2,9)]).value_counts())
    
    (1, 2)    2
    (1, 4)    1
    (6, 7)    1
    (2, 9)    1
    dtype: int64
    

提交回复
热议问题