How can I find the duplicates in a Python list and create another list of the duplicates? The list only contains integers.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I would do this with pandas, because I use pandas a lot
Gives
Probably isn't very efficient, but it sure is less code than a lot of the other answers, so I thought I would contribute
We can use
itertools.groupby
in order to find all the items that have dups:The output will be:
collections.Counter is new in python 2.7:
In an earlier version you can use a conventional dict instead:
this is the way I had to do it because I challenged myself not to use other methods:
so your sample works as:
Using pandas: