Python - Count occurrences of certain ranges in a list

后端 未结 4 718
一个人的身影
一个人的身影 2021-01-01 05:00

So basically I want to count the number of occurrences a floating point appears in a given list. For example: a list of grades (all scores out of 100) are inputted by the u

4条回答
  •  礼貌的吻别
    2021-01-01 05:43

    If you are fine with using the external library NumPy, then you just need to call numpy.histogram():

    >>> data = [82, 85, 90, 91, 70, 87, 45]
    >>> counts, bins = numpy.histogram(data, bins=10, range=(0, 100))
    >>> counts
    array([0, 0, 0, 0, 1, 0, 0, 1, 3, 2])
    >>> bins
    array([   0.,   10.,   20.,   30.,   40.,   50.,   60.,   70.,   80.,
             90.,  100.])
    

提交回复
热议问题