i would like to know why this code doesnt work... I dont know why it doesnt allow me to use the if condition in this case.
a = np.array(range(30)).reshape(3,10)
a[:,1] = -1 #random values set to -1
a[:,6] = -1
a[:,7] = -1
print(a)
b = []
for i in a:
if i !=-1:
b.append(True)
b --> ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I would like to put the remaining values of a into b. Thank you for your help.
CodePudding user response:
The reason you are getting this error is because the element i
in a
is still a numpy
array. The !=
operator used in this way needs a single element, so to access these you need to put an additional layer for j in i:
and then change the following line with if j !=1:
. So for example
import numpy as np
a = np.array(range(30)).reshape(3,10)
a[:,1] = -1 #random values set to -1
a[:,6] = -1
a[:,7] = -1
b = []
for i in a:
for j in i:
if j !=-1:
b.append(True)
This prints for a
:
[[ 0 -1 2 3 4 5 -1 -1 8 9]
[10 -1 12 13 14 15 -1 -1 18 19]
[20 -1 22 23 24 25 -1 -1 28 29]]
And for b
we get:
[True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]
As far as the goal of this code, I'm not sure what output you expect, but this is the error in terms of making it run.
CodePudding user response:
I think the issue is that when you iterate through a
in the for loop, what you're iterating through is not each element in a
, but each sub-array.
If you add print(len(a))
, you'll see that you're getting 3 as the result, not the expected 30. So, when you iterate through a
(i
being the sub-array within a
) and then test to see if the value is -1
, what you're asking python to do is to see if an array of [0,-1,2,3,4,5,-1,-1,8,9]
is equal to -1. An array can't be equal to an integer because it's an array not an integer. That error is telling you that when it says:
The truth value of an array with more than one element is ambiguous.
So, to answer your question, you can use if
there, you you need to be comparing two like objects (ints to ints, strings to strings, etc.). You're trying to test if multiple values equal a single value and that's where python gets mad at you.
Your question is still a bit unclear as to what you want b to look like, but if you just want to return all the non -1 values in a
in roughly the same shape as a
, you could do a double for loop:
## Create blank output array
b = []
## First loop goes through the length of a, which will be 3, meaning we're iterating over each sub-array
for i in range(len(a)):
## Add a blank sub array to our output array
b.append([])
## Start a second loop that iterates through each element in the sub-array, keyed to the current index in our loop through 0-2
for x in a[i]: ## a[i] in the first loop is a[0], a[1] in the second run, etc.
if x != -1:
gg[i].append(x) ## Now append the value of x if it's not -1 to the sub-array we created at index i in b
print(b)
>>> [[0, 2, 3, 4, 5, 8, 9], [10, 12, 13, 14, 15, 18, 19], [20, 22, 23, 24, 25, 28, 29]]
There is also a much less ugly way to do this with list comprehension
b = [[y for y in x if y != -1] for x in a]
print(b)
>>> [[0, 2, 3, 4, 5, 8, 9], [10, 12, 13, 14, 15, 18, 19], [20, 22, 23, 24, 25, 28, 29]]