T
T
Ternick2021-05-17 20:18:14
Python
Ternick, 2021-05-17 20:18:14

How to solve ValueError: The truth value...?

There is a large array, when I try to sort through it, I get

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


THE CODE:

cleared_img_data = array(["+" if rgb != bg_color else "." for rgb in img_data])


As far as I understand, numpy hints that there is some function that makes this kind of thing faster ?

I have never used numpy in particular, and therefore I don’t really know anything, maybe this is not even the last question)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-05-17
@Ternick

numpy hints that the expression rgb != bg_color will return not a boolean value, but an array of boolean values ​​- the results of comparing each element. In other words,
print(numpy.array([1, 2, 3]) != numpy.array([1, 2, 4])) will print [False, False, True]
As a result, it is not clear whether such an array should be considered true or false if it has several values ​​and they are different.
The warning tells you how to fix it.
If you need any mismatch, then write (rgb != bg_color).any().
If you want all three color channels to be different, then (rgb != bg_color).all().
But to be honest, cleared_img_data is some weird array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question