I am trying to search in a 2D numpy array for a specific value, the get_above method returns a list of coordinates above the character 'initial char'
def get_above(current, wordsearch):
list_of_current_coords = get_coords_current(current, wordsearch)
#print(list_of_current_coords)
length = len(list_of_current_coords)
first_coords = []
second_coords = []
for x in range(length):
second = list_of_current_coords[x][1]
new_first = list_of_current_coords[x][0] - 1
first_coords.append(new_first)
second_coords.append(second)
combined = [first_coords, second_coords]
above_coords = []
for y in range(length):
lst2 = [item[y] for item in combined]
above_coords.append(lst2)
return above_coords
def search_above(initial_char, target, matrix):
above_coords = get_above(initial_char, matrix)
length = len(above_coords)
for x in range(length):
if matrix[above_coords[x]] == target:
print(above_coords[x])
else:
print('not found')
And I get this error when calling the function:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Any help would be appreciated!
The ValueError is caused by an array comparison in the
if
statement.Lets make a simpler test case:
The
m==3
test produces a boolean array. That can't be used in anif
context.any
orall
can condense that array into one scalar boolean:So in
look at
matrix[above_coords[x]] == target
, and decide exactly how that should be turned into a scalar True/False value.