How do I find the duplicates in a list and create another list with them?

前端 未结 30 1590
梦谈多话
梦谈多话 2020-11-22 00:56

How can I find the duplicates in a Python list and create another list of the duplicates? The list only contains integers.

30条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 01:02

    I would do this with pandas, because I use pandas a lot

    import pandas as pd
    a = [1,2,3,3,3,4,5,6,6,7]
    vc = pd.Series(a).value_counts()
    vc[vc > 1].index.tolist()
    

    Gives

    [3,6]
    

    Probably isn't very efficient, but it sure is less code than a lot of the other answers, so I thought I would contribute

提交回复
热议问题