I’ve been trying to write some code which will add the numbers which fall into a certain range and add a corresponding number to a list. I also need to pull the range from a cumsum range.
numbers = []
i=0
z = np.random.rand(1000)
arraypmf = np.array(pmf)
summation = np.cumsum(z)
while i < 6:
index = i-1
a = np.extract[condition, z] # I can't figure out how to write the condition.
length = len(a)
length * numbers.append(i)
I’m not entirely sure what you’re trying to do, but the easiest way to do conditions in
numpyis to just apply them to the whole array to get a mask:Then you can use, e.g.,
extractormaif necessary—but in this case, I think you can just rely on the fact thatTrue==1andFalse==0and do this:After all, if all you’re doing is summing things up,
0is the same as not there, and you can just replacelenwithcount_nonzero.For example: