Find duplicates in a list and count them ...

Benjamin Kaplan bsk16 at case.edu
Thu Mar 26 18:12:29 EDT 2009


On Thu, Mar 26, 2009 at 5:14 PM, <Paul.Scipione at aps.com> wrote:

> Hi D'Arcy J.M. Cain,
>
> Thank you.  I tried this and my list of 76,979 integers got reduced to a
> dictionary of 76,963 items, each item listing the integer value from the
> list, a comma, and a 1.  I think what this is doing is finding all integers
> from my list that are unique (only one instance of it in the list), instead
> of creating a dictionary with integers that are not unique, with a count of
> how many times they occur.  My dictionary should contain only 11 items
> listing 11 integer values and the number of times they appear in my original
> list.
>


Not all of the values are 1. The 11 duplicates will be higher. Just iterate
through the dict to find all keys with values > 1.

>>> icounts
{1: 2, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 5, 8: 3, 9: 1, 10: 1, 11: 1}

Python 2.x :
>>> dups = {}
>>> for key, value in icounts.iteritems() :
...    if value > 1 :
...       dups[key] = value
...
>>> dups
{8: 3, 1: 2, 7: 5}


Python 3.0 :
>>> dups = {key:value for key, value in icounts.items() if value > 1}
>>> dups
{8: 3, 1: 2, 7: 5}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090326/28eaf7b3/attachment-0001.html>


More information about the Python-list mailing list