get most common number in a list with tolerance

Tim Rowe digitig at gmail.com
Fri Feb 20 11:33:20 EST 2009


2009/2/20 Astan Chee <astan.chee at al.com.au>:
> 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)]

Why only the points 10, 20 and 30? what has happened to (3,11), for
example? (10, 11 and 12 are all within 11+/-5)

It seems you are trying to do one of two things. Either you are trying
to form a histogram with data bands +/-tolerance, or you are trying to
do something like kernel smoothing with a rectangular kernel.

What would you expect the output to be if the data set were
[10,30,20,20,11,12,13] and the tolerance were 2?

-- 
Tim Rowe



More information about the Python-list mailing list