Is there any efficient way in python to count the times an array of numbers is between certain intervals? the number of intervals i will be using may get quite large
like:
mylist = [4,4,1,18,2,15,6,14,2,16,2,17,12,3,12,4,15,5,17]
some function[mylist, startpoints]:
# startpoints = [0,10,20]
count values in range [0,9]
count values in range [10-19]
output = [9,10]
asked May 31, 2010 at 23:46
calccryptocalccrypto
8,23121 gold badges66 silver badges99 bronze badges
you will have to iterate the list at least once.
The solution below works with any sequence/interval that implements comparision [, etc] and uses
bisect
algorithm to find the correct point in the interval, so it is very fast.
It will work with floats, text, or whatever. Just pass a sequence and a list of the intervals.
from collections import defaultdict
from bisect import bisect_left
def count_intervals[sequence, intervals]:
count = defaultdict[int]
intervals.sort[]
for item in sequence:
pos = bisect_left[intervals, item]
if pos == len[intervals]:
count[None] += 1
else:
count[intervals[pos]] += 1
return count
data = [4,4,1,18,2,15,6,14,2,16,2,17,12,3,12,4,15,5,17]
print count_intervals[data, [10, 20]]
Will print
defaultdict[, {10: 10, 20: 9}]
Meaning that you have 10 values