Find how many numbers are above a specific value in Numpy Array

时间:2016-04-04 18:28:07

标签: arrays numpy random input int

The code I have at this point is thus:

import numpy as np
count=0
other=0
inp=input('Enter number of #s: ')
bottom=input('Enter min: ')
upper=input('Enter max: ')
middle=input('Provide middle number: ')
#for x in range(1):
ma=np.random.random_integers(bottom,upper, size=(inp,))
print np.sort(ma)

    #if (ma > middle):
        #print 'fart'
    #elif (ma < middle):
        #other=other+1
#print count, "numbers over middle"
#print other, "numbers under middle"

I have figured out how to create a custom array in length and values. However, I need to be able to determine how many numbers fall above a user defined middle point. I was originally doing this with lists, but the requirement has changed to array

1 个答案:

答案 0 :(得分:2)

middle_num = float(middle)
count = np.sum(ma > middle_num)

The way this works is that ma > middle_num returns a matrix of true, false answers whether or not ma is greater than middle_num then you can sum all of the true false values because python considers true == 1 and false == 0