# How to sort list of numbers into categories with specific cutoff?

anybody can help with the way how to sort my numbers in different categories:

``````3.83
3.93
3.48
2.96
3.66
2.79
3.17
``````

I want to find the way how to return the number of appearances, something like:

``````1.5-2: 0
2-2.5: 0
2.5-3.2: 3
3.2-4: 4
``````
• This seems vaguely on the edge of being 1) a homework problem, which depending on the day you may find various responses to and 2) is teetering on the edge of being related to Ubuntu at all. Apr 23, 2020 at 8:19
• No its not a homework, just something for personal use, I dont expect somebody to answer the question but to give a bit of clue how to do it. But its fine, thats you personal opinion and I got nothing against it, if you think this question is inappropriate you can vote for it to close. Apr 23, 2020 at 8:44
• This is a histogram binning problem. See for example numpy.histogram Apr 23, 2020 at 10:32
• Great, you can put it in the answer as well, Ill give you a upvote as it was helpful! Apr 23, 2020 at 13:33

What you are describing is a histogram binning problem. In python, you can use the numpy `histogram` function, ex.:

``````>>> import numpy as np
>>> print np.histogram([3.83,3.93,3.48,2.96,3.66,2.79,3.17],[2,2.5,3.2,4])
(array([0, 3, 4]), array([ 2. ,  2.5,  3.2,  4. ]))
>>>
``````

Documentation:

Using the `histogram.py` util, which includes an option for binning numbers in a custom list of buckets:

``````histogram.py --no-mvsd -m 1.5 -x 4 -B 2,2.5,3.2,4  <  numbers.txt
``````

Output:

``````# NumSamples = 7; Min = 1.50; Max = 4.00
# each ∎ represents a count of 1
1.5000 -     2.0000 [     0]:
2.0000 -     2.5000 [     0]:
2.5000 -     3.2000 [     3]: ∎∎∎
3.2000 -     4.0000 [     4]: ∎∎∎∎
``````

Note: the current version (v0.3.1) of the above util is a bit temperamental. One might suppose `histogram.py --no-mvsd -B 1.5,2,2.5,3.2,4 < numbers.txt` would be sufficient, but the resulting output then omits the `0` count rows, printing only the two rows with `3` and `4`. Adding a min `-m 1.5` and max `-x 4`, plus paring the `-B` list are needed to produce the desired output.