Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Find duplicates in a list and count them ...

Reply
Thread Tools

Re: Find duplicates in a list and count them ...

 
 
D'Arcy J.M. Cain
Guest
Posts: n/a
 
      03-26-2009
On Thu, 26 Mar 2009 12:22:27 -0700
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.


icount = {}
for i in list_of_ints:
icount[i] = icount.get(i, 0) + 1

Now you have a dictionary of every integer in the list and the count of
times it appears.

--
D'Arcy J.M. Cain <(E-Mail Removed)> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      03-26-2009
"D'Arcy J.M. Cain" <(E-Mail Removed)> writes:
> icount = {}
> for i in list_of_ints:
> icount[i] = icount.get(i, 0) + 1


from collections import defaultdict

icount = defaultdict(int)
for i in list_of_ints:
icount[i] += 1
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Find duplicates in a list and count them ... Paul.Scipione@aps.com Python 1 03-26-2009 11:06 PM
Re: Find duplicates in a list and count them ... MRAB Python 0 03-26-2009 10:54 PM
Possible to sort and omit duplicates in a list using javascript Laphan ASP General 2 02-12-2008 06:23 PM
Can some one Explain How To Find File and Leech them Accully How to find all Articals Related to that binary callejachris@tpg.com.au Computer Support 2 12-04-2005 02:15 PM
QUESTION: Good data structure for list within list with duplicates? basi Ruby 4 08-01-2005 07:30 PM



Advertisments