Python: Can I use the AND operator in an array index?

381 views Asked by At

I have a list of values, x, and y:

x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 3, 5, 8, 44, 8,0]

Which I convert to arrays:

x = np.array(x)
y = np.array(y)

When I try to print the corresponding y-values for a specified range of x, say x < 5, I get the correct output:

y_new = y[x < 5]
print(y_new)

Output: [1 3 5 8]

However, when I try to retrieve the values of y for 1 < x < 5, I get the following error:

y_1_5 = y[1 < x < 5]
y_15 = y[x > 1 and x < 5]
print(y_1_5)
print(y_15)

Ouput: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

How do I fix this error because I want to print values of y for 1 < x < 5? Your help will be much appreciated. Thank you in advance.

0

There are 0 answers