I have a list of numbers looking like this:
numbers = [406.82, 406.93, 406.80, 406.89,
443.22, 443.27,
415.01, 415.12, 415.2,
443.71, 443.83,
451.05, 451.14]
I want to group based on the how close they are:
numbers_grouped = [[406.82, 406.93, 406.80, 406.89]
[443.22, 443.27]
[415.01, 415.12, 415.2]
[443.71, 443.83]
[451.05, 451.14]]
I tried this method but it doesn't seem to work,
- sorting it by ascending order
- then subtracting each number with its neighbouring numbers
- if the number is less than 0.1 then it will be grouped else not
But is there a better method to solve this problem?