get most common number in a list with tolerance

A

Astan Chee

Hi,
I have a list that has a bunch of numbers in it and I want to get the
most common number that appears in the list. This is trivial because i
can do a max on each unique number. What I want to do is to have a
tolerance to say that each number is not quite unique and if the
difference from other numbers is small, than it can be counted together.
This is what I have to get the common number (taken from the internet
somewhere):

l = [10,30,20,20,11,12]
d = {}
tolerance = 5
for elm in l:
d[elm] = d.get(elm, 0) + 1
counts = [(j,i) for i,j in d.items()]



This of course returns a list where each of them is unique

[(1, 12), (1, 10), (1, 11), (2, 20), (1, 30)]

but I am expecting a list that looks like this:

[(3, 10), (2, 20), (1, 30)]

What do I need to add?
Thanks for any help.
Cheers
Astan
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top