Why doesn't the following code work?
import numpy
grid = numpy.matrix([[1,0,1,1],[1,1,0,0],[1,0,1,0],[0,0,0,1]])
i = 0
for line in grid:
for block in line:
if block == 1:
i += 1
print("Grid has " + str(i) + " times number 1")
I thought it's gonna first cycle through every line and then every element of the line and compare it with 1, but I get this error:
Traceback (most recent call last):
File "python", line 7, in <module>
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
numpy.arrayinstead ofnumpy.matrix, you wouldn't have had this problem. Seriously,numpy.matrixisn't worth it.