So let me start with the issue.
When you compare a numpy.array
with more than one element, you could get following result: [True, False]
.
The if
-statement does not know how to make sense of that and therefore you need to do exactly what the exception tells you to do, you need to add .all()
at the end of the condition like this: ((np.array(e[i][j+1]) - np.array(e[i][j])) == [0,1]).all()
.
In the following i assume, based on your code, that you are looking for the rows that only contain columns with the difference [0,1]
between them and not for the columns. (If i am mistaken, i am sorry. I just woke up.)
Let's move on.
Still there is one problem. With your solution you will get this result:
([0, 0], [0, 1], [0, 2])
([0, 0], [0, 1], [0, 2])
([0, 0], [0, 1], [0, 3])
([0, 0], [1, 0], [1, 1])
([0, 0], [1, 0], [1, 1])
([0, 0], [1, 0], [1, 1])
But the result you want is this:
([0, 0], [0, 1], [0, 2])
Hence here is my solution based on this post.
import numpy as np
class ContinueEx(Exception):
pass
continue_ex = ContinueEx()
_list = e= [([0, 0], [0, 1], [0, 2]),
([0, 0], [0, 1], [0, 3]),
([0, 0], [0, 2], [1, 0]),
([0, 0], [0, 2], [1, 1]),
([0, 0], [0, 2], [1, 1]),
([0, 0], [0, 2], [1, 1]),
([0, 0], [0, 2], [1, 2]),
([0, 0], [0, 2], [1, 2]),
([0, 0], [0, 2], [2, 0]),
([0, 0], [1, 0], [1, 1]),
([0, 0], [1, 0], [1, 1]),
([0, 0], [1, 0], [1, 1]),
([0, 0], [1, 2], [2, 0])]
for _inner_list in range(0,len(_list)):
try:
for _list_element in range(1, len(_list[_inner_list])):
if not ((np.array(_list[_inner_list][_list_element]) - np.array(_list[_inner_list][_list_element-1]) == np.array([0,1]))).all():
raise continue_ex
print(_list[_inner_list])
except ContinueEx:
continue
I hope it was helpful.