I'm generating a series of values and would like to bin them. I'd rather not use numpy or the like. Is there something more pythonic than:
bins = [20,30,40]
results = [0,0,0,0]
for _ in range(iterations):
x = somefunction()
for n, bin in enumerate(bins):
if x < bin:
results[n] += 1
break
else:
results[-1] += 1
final = [100 * r / float(iterations) for r in results]
print(final)
You could use something like that:
r = [0] * (len(bins) + 1)
for _ in xrange(iterations):
r[next((i for i, bin in enumerate(bins) if somefunction() < bin), -1)] += 1
or alternatively a counter:
n = len(bins)
from collections import Counter
c = Counter()
c.update(
next((i for i, bin in enumerate(bins) if somefunction() < bin), n)
for _ in xrange(iterations)
)
It would be better/faster (for larger arrays) to use a binary search algorithm instead of a linear search algorithm.
That is,
def binm(rr,ra):
ih=len(ra)-1
il=0
if rr<ra[il]: return il
while (ih-il>1):
ie=(ih+il)/2
if rr<ra[ie]:
ih=ie
else:
il=ie
return ih
bins = [20,30,40]
results = [0,0,0,0]
for _ in range(iterations):
x = somefunction()
ib=binm(x,bins)
results[ib]+=1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.