Find duplicates in a list and count them ...
marduk at letterboxes.org
Thu Mar 26 20:54:22 CET 2009
On Thu, 2009-03-26 at 12:22 -0700, Paul.Scipione at aps.com wrote:
> I'm a newbie to Python. I have a list which contains integers (about
> 80,000). I want to find a quick way to get the numbers that occur in
> the list more than once, and how many times that number is duplicated
> in the list. I've done this right now by looping through the list,
> getting a number, querying the list to find out how many times the
> number exists, then writing it to a new list. On this many records it
> takes a couple of minutes. What I am looking for is something in
> python that can grab this info without looping through a list.
Why not build a histogram?
$ cat test.py
from random import randint
l = list()
for i in xrange(80000):
hist = dict()
for i in l:
hist[i] = hist.get(i, 0) + 1
for i in range(10):
print "%s: %s" % (i, hist.get(i, 0))
$ time python test.py
More information about the Python-list