As stated in the title. I am trying to compare two large(21x21) grids containing elments of single dots and single dashes to see if each element at each index is the same. Using array1 == array2
produces the following error:
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
However, I have not encountered a good explanation of the a.all/a.any syntax. What goes before the dot? What params do they take?
Edit: I have been avoiding using NumPy, but theres no way around it. NumPy is imported. Any ideas?
Portion of the code where I am creating a default grid of dots and dashes:
defaultgrid = [['.' for x in range(width)] for y in range(height)]
for x in range(1, 21, 2):
defaultgrid[x] = [" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " "]
for x in range(0, 21, 2):
defaultgrid[x] = [".", " ", ".", " ", ".", " ", ".", " ", ".", " ", ".", " ", ".", " ", ".", " ", ".", " ", ".",
" ", "."]
How about comparing the strings of the arrays?
Str(arr1) == str(arr2)
If you're sure that you want to use Numpy then a way to solve your error The truth value of an array.....
is following:
If you have two numpy arrays arr1
and arr2
then everywhere compare them for equality not by if arr1 == arr2:
but by if np.array_equal(arr1, arr2):
.
Alternatively if you're sure that your two arrays have same size (dimensionality) and type then you can compare them for equality also like this if np.all(arr1 == arr2):
.
If you don't use Numpy then two nested lists can be just compared as if arr1 == arr2:
.
You were saying that you used numpy only to do np.copy()
. To make a deep copy of a list you can use standard copy.deepcopy() (you have to import copy
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.