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

Discussion in 'Python' started by Paul.Scipione@aps.com, Mar 26, 2009.

Guest

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.

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.

This message is for the designated recipient only and may contain confidential, privileged or proprietary information. If you have received it in error, please notify the sender immediately and delete the original and any copy or printout. Unintended recipients are prohibited from making any other use of this e-mail. Although we have taken reasonable precautions to ensure no viruses are present in this e-mail, we accept no liability for any loss or damage arising from the use of this e-mail or attachments, or for any delay or errors or omissions in the contents which result from e-mail transmission.

Advertisements

On Mar 27, 8:14 am, 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 doubt this very much. Please show:
(a) your implementation of D'Arcy's suggestion
(b) the code you used that lead you to the conclusion that all counts
were 1. See example below.

> 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.

The only way of getting your desired result is to get a dict of counts
and then to filter out the ones where the count is greater than one.
D'Arcy appears to have presumed that it was not necessary to show the
second stage

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!