Find duplicates in a list and count them ...

Albert Hopkins marduk at letterboxes.org
Thu Mar 26 16:00:01 EDT 2009


On Thu, 2009-03-26 at 15:54 -0400, Albert Hopkins wrote:
[...]

> $ cat test.py 
> from random import randint
> 
> l = list()
> for i in xrange(80000):
>     l.append(randint(0,10))
      ^^^^^^^^^^^^^^^^^^^^^^^
should have been:
      l.append(randint(0,9))


> 
> 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 
> 0: 7275
> 1: 7339
> 2: 7303
> 3: 7348
> 4: 7206
> 5: 7323
> 6: 7230
> 7: 7348
> 8: 7166
> 9: 7180
> 
> real	0m0.533s
> user	0m0.518s
> sys	0m0.011s





More information about the Python-list mailing list