Let's say I've got a matrix like this:
mat1 = np.array([1,0,1], [1,1,0], [0,0,0]);
And I've got another one like this:
mat2 = np.array([0,1,0], [0,0,1], [1,1,1]);
I want to detect if something like
np.add(mat1, mat2);
has only 1's or 0's, namely some 1's and some 0's, all 0's, or all 1's.
n.b. - Comment your code.
解决方案
How about this:
>>> def check(matrix):
... # flatten up the matrix into one single list
... # and set on the list it should be [0,1] if it
... # contains only 0 and 1. Then do sum on that will
... # return 1
... if sum(set(sum(matrix,[]))) > 1:
... return False
... return True
...
>>>
>>> check([[1,0,1], [1,1,0], [0,0,0]])
True
>>> check([[1,0,1], [1,1,0], [0,0,2]])
False
>>> check([[1,0,1], [1,1,0], [0,0,3]])
False
>>>