# Fastest way to find the number of elements within a certain interval using two lists

## Question:

I have two lists called intervals and TIME. For each t in intervals, I define an interval (t-dt/2 and t+dt/2) and I want to calculate how many elements in TIME fall into that interval. Basically what I do is calculate the time bins. The below code is doing that, but I have a problem.

``````counts = []
for t in intervals:
# Define time interval bounds
lower_bound = t - dt / 2
upper_bound = t + dt / 2
# Count number of elements in TIME that fall within interval
count = np.sum(np.logical_and(TIME > lower_bound, TIME < upper_bound)) # Counts the true
counts.append(count)
``````

This generates boolean list and count on the number. However, I need a numerical value where I also need to divide the number by list3[n] where n is the corresponding value for true return.

For example, if TIME[n] > lower_bound, TIME[n] < upper_bound, I just don’t want to be counted as 1, but I also want to divide it by list3[n].

I’m not sure how can I implement it here or if there is a faster way to do it. Using brute force takes a lot of time since these lists are pretty big. This was the fastest I could find, but now I don’t know how to divide each corresponding true return by corresponding list3[n]. Thanks for any help!

Edit: To give an example, say that t in interval is 1 and dt is 0.1. I want to find which elements in TIME list stay in that interval `[0.9, 1.1]`. The above code does that and counts how many elements of TIME stay in that interval.

However, I also have another list, called list3 that needs to divide corresponding TIME values. For example, if `TIME=[0.8, 0.9, 1.0, 1.1, 1.2]` and `list3=[0.1, 0.2, 0.3, 0.4, 0.5]` then the following is needed,

`sum=1/list3[1]+1/list3[2]+1/list3[3]`. Note that I do not include `TIME[0]` and `TIME[4]` since it is not in the interval.

this can easily be done using np.where

``````reciprocal_list3 = 1/list3  # do this line outside of the loop
count = np.sum(
np.where(np.logical_and(TIME > lower_bound, TIME < upper_bound),
reciprocal_list3, 0)
)
``````

simply put, whenever the logical value of the comparison is `True`, it will grab that same index from reciprocal_list3 to use in the sum.

Categories: questions
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.