0

I'm writing a code which takes a list of numbers, some of which are very close or equal to other elements of the list, for example [0.1,0.101,0.2,0.499,0.5], then returns a smaller list of the main numbers the elements of this list fall close to, so in this case [0.1005,0.2,0.4995].

At the moment my code would return the first number from each category from the list, so here it returns [0.1,0.2,0.499], but I need more accuracy.

Here's my current code:

def listsort(x):
    deletelist = []
    for i in range(len(x)):
        for j in range(i+1,len(x)):
            if abs(x[i]-x[j])<1e-5:
                deletelist.append(x[j])
    for j in deletelist:
        x.remove(j)
    return x
  • 2
    seems like you should group them together first, and then take the average of each subgroup. – Chris Maes Apr 24 '20 at 11:49
  • 1
    If you first [cluster your values](https://stackoverflow.com/questions/15800895/finding-clusters-of-numbers-in-a-list) the rest is just taking the mean of each group. – Cory Kramer Apr 24 '20 at 11:49
  • This question has been asked. Please see answers here: https://stackoverflow.com/a/14783980/7922348 – quantotto Apr 24 '20 at 12:06

0 Answers0