1

I have recently started working in numpy. I am trying to test if a 2d array contains a specific subarray. The code below returns an error. How can I fix this?

import numpy as np

testArray = np.array([[None, 0], [None, 0], [None, 0], [None, 0], [None, 0], [None, 0], [None, 0], [None, 0], [None, 0]])

for i in range(len(testArray)):
    if (testArray[i] == [None, 0]):
        print(i)
0

3 Answers 3

1

Without iterating, you can use all:

>>> testArray[(testArray == [None,0]).all(1)]
array([[None, 0],
       [None, 0],
       [None, 0],
       [None, 0],
       [None, 0],
       [None, 0],
       [None, 0],
       [None, 0]], dtype=object)

Or if you just want to see whether that subarray exists, use any in addition:

>>> (testArray == [None,0]).all(1).any()
True
Sign up to request clarification or add additional context in comments.

Comments

0

The error you are seeing is a Value Error. When you compare the numpy arrays, you get an array consisting of boolean values. The problem was that you were using that array to as your condition, which resulted in the value error since the truth-value of arrays is ambiguous. You can resolve by using .any or .all, depending on whether you care if all the elements are present in your array.

Try this:

testArray = np.array([[None, 0], [None, 0], [None, 0], [None, 0], [None, 0], [None, 0], [None, 0], [None, 0], [None, 0]]) 

for i in range(len(testArray)): 
    containsValue = (testArray[i] == [None, 0]).all()
    if (containsValue): 
        print(i) 

4 Comments

You may as well be using lists, this doesn't use any numpy features. It will actually be slower than using lists.
They didn't ask to optimize for speed, they just said they had an error in their code.
Presumably they would not be layering in an external dependency to make their code slower than native Python.
I can't pretend to know what the asker is thinking. They had an error in their code and my answer resolves said error.
0

Well, one of your '[None, 0]' is a list which you are comparing to an array which doesn't make much sense. So if you want to fix the code then you can access the values by:

import numpy as np

testArray = np.array([[None, 0], [None, 0], [None, 0], [None, 0], [None, 0], [None, 0], [None, 0], [None, 0], [None, 0]])

for i in range(len(testArray)):
    if (testArray[i][0] == None and testArray[i][1] == 0):
        print(i)

7 Comments

As the other answer, there's absolutely no use for numpy in this approach
Where did they ask to use numpy in the approach?
It's a numpy array and they state that they're learning numpy?
one is a list the other one a numpy array, I will adjust my answer
He specifically stated that he is using numpy. I just fixed the code, I don't know why he wants to do it that way. But he never told us what he actually wants to achieve so, I do not know.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.