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

Discussion in 'Python' started by D'Arcy J.M. Cain, Mar 26, 2009.

  1. On Thu, 26 Mar 2009 12:22:27 -0700
    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 = 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 <> | 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.
     
    D'Arcy J.M. Cain, Mar 26, 2009
    #1
    1. Advertisements

  2. D'Arcy J.M. Cain

    Paul Rubin Guest

    "D'Arcy J.M. Cain" <> writes:
    > icount = {}
    > for i in list_of_ints:
    > icount = icount.get(i, 0) + 1


    from collections import defaultdict

    icount = defaultdict(int)
    for i in list_of_ints:
    icount += 1
     
    Paul Rubin, Mar 26, 2009
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stern

    Find duplicates

    Stern, Oct 20, 2003, in forum: Java
    Replies:
    12
    Views:
    15,628
    jyotidayal
    Dec 5, 2010
  2. da_rod_father
    Replies:
    3
    Views:
    6,954
    Hal Rosser
    Mar 18, 2006
  3. Anonieko

    HttpHandlers - Learn Them. Use Them.

    Anonieko, Jun 15, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    785
    tdavisjr
    Jun 16, 2006
  4. christof hoeke

    delete duplicates in list

    christof hoeke, Oct 29, 2003, in forum: Python
    Replies:
    22
    Views:
    1,032
    Michael Hudson
    Oct 31, 2003
  5. Replies:
    1
    Views:
    588
    John Machin
    Mar 26, 2009
  6. Laphan
    Replies:
    2
    Views:
    250
    Laphan
    Feb 12, 2008
  7. why the lucky stiff
    Replies:
    5
    Views:
    359
    why the lucky stiff
    Sep 22, 2004
  8. basi
    Replies:
    4
    Views:
    331
    Wayne Vucenic
    Aug 1, 2005
Loading...