Carlo Silanu
Carlo Silanu

Reputation: 79

How to print the number of rows that have all their values between a number

Hi so I am fairly new to python and I am trying to find the number of rows that have all their values between 0-50 (both numbers inclusive) from a numpy array that i created.

array([[39, 42, 48, -5, -2, -2, 34,  4, 14, 16],
       [45, 31, 18,  1, 19, 19,  7, -4, 33, 34],
       [18, 41, 19, 12, 32, 20,  8,  3,  4, 15],
       [46, 11, 46,  0, 10, 42, -5, 13, 30, 19],
       [44, 46, 24, 14, 14,  9, 34, 27, -4,  4],
       [27, 26,  5, 47, 18, 30,  6, 45, 23, 29],
       [-5, -5, 31, 48,  0, 33, 35, 47, 12, 10],
       [-1, 36, 37, 26, -4, -4, 34, 36, 30, 33],
       [ 6, 41, 13, 22, -5,  9, 30, 48,  7, 37],
       [15,  6, -1,  1, -1, 42, 47, -2,  7, 31],
       [47, 35,  9, 10, 15, 30, 18, 10,  8, 48],
       [16, 43, 44,  0, 36, 30, -5, 26,  0, 25],
       [-5, 44, 45, 31, 29, 43, 24, -2, 29, 37],
       [ 8, 43, 34, 16,  4, -5,  5, 45, 38, 18],
       [-3, 29, 30, 25, -2, 13, 41, 30, 15, 12],
       [45, 22,  9, 36, -4, 31,  5, 17, 38, 35],
       [ 6, -3, 46, 11, 27, 49, -5, 33, 14, 41],
       [37, 46, 35,  8, 25, 19, -3, -2, 25, 29],
       [38,  8, 43, 35,  3, 14, 26,  3, 21, -3],
       [47, -2, 39,  9, 27, -1, -2, 40,  6, 17]])

i want the code to show me 3 because there is only 3 row from the array that have all their values between 0-50. Can anybody help me do this?

Upvotes: 0

Views: 61

Answers (4)

Silvestre Bahi
Silvestre Bahi

Reputation: 106

This can be done easily and without iterating by using np.all and np.count_nonzero:

cond = np.all((x < 50) * (x > 0), axis=1)  # check which rows are between 0-50
count = np.count_nonzero(cond)  # counts the number of corresponding rows

Upvotes: 1

kederrac
kederrac

Reputation: 17322

you can use the built-in function sum with numpy.all:

sum(np.all((a >= 0) & (a <= 50), axis=1)) # a is your np.array

output:

3

Upvotes: 1

IrtzaSuhail
IrtzaSuhail

Reputation: 97

x = array(...)
count = 0
for y in x:
    if (min(y) > -1 and max(y) < 51):
        count += 1

Something like this should give you what you want.

Upvotes: 1

marcos
marcos

Reputation: 4510

You can iterate over each row and check if the values are between the numbers, for example:

from numpy import array

matrix = array([[39, 42, 48, -5, -2, -2, 34,  4, 14, 16],
       [45, 31, 18,  1, 19, 19,  7, -4, 33, 34],
       [18, 41, 19, 12, 32, 20,  8,  3,  4, 15],
       [46, 11, 46,  0, 10, 42, -5, 13, 30, 19],
       [44, 46, 24, 14, 14,  9, 34, 27, -4,  4],
       [27, 26,  5, 47, 18, 30,  6, 45, 23, 29],
       [-5, -5, 31, 48,  0, 33, 35, 47, 12, 10],
       [-1, 36, 37, 26, -4, -4, 34, 36, 30, 33],
       [ 6, 41, 13, 22, -5,  9, 30, 48,  7, 37],
       [15,  6, -1,  1, -1, 42, 47, -2,  7, 31],
       [47, 35,  9, 10, 15, 30, 18, 10,  8, 48],
       [16, 43, 44,  0, 36, 30, -5, 26,  0, 25],
       [-5, 44, 45, 31, 29, 43, 24, -2, 29, 37],
       [ 8, 43, 34, 16,  4, -5,  5, 45, 38, 18],
       [-3, 29, 30, 25, -2, 13, 41, 30, 15, 12],
       [45, 22,  9, 36, -4, 31,  5, 17, 38, 35],
       [ 6, -3, 46, 11, 27, 49, -5, 33, 14, 41],
       [37, 46, 35,  8, 25, 19, -3, -2, 25, 29],
       [38,  8, 43, 35,  3, 14, 26,  3, 21, -3],
       [47, -2, 39,  9, 27, -1, -2, 40,  6, 17]])

output = sum(1 for row in matrix if all(0 <= value <= 50 for value in row))
print(output)
>>> 3

Upvotes: 2

Related Questions