1

I am trying to print only the lines with the elements where the differences between columns(j) and columns (j+1) are equal to [0,1]). This means, in the given example I would keep only the first column (this dataset is just a little sample of a bigger one):

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 i in range(0,12):
    for j in range(0,2):
        if ((np.array(e[i][j+1]) - np.array(e[i][j])) == [0,1]):
            print (e[i])

pyt

Thanks in advance!

Miguel Gonzalez
  • 398
  • 1
  • 12
  • What would you subtract [0,2] (e[0][2] since e[0][3] doesn't exist) from? Do you want that to be printed? Also I think you made a tiny mistake in the way you created the numpy array – Anna Kallivayalil Jul 17 '20 at 08:18
  • "I am trying to print only the lines with the elements where the differences between columns(j) and columns (j+1) are equal to [0,1])." So, of the results from the subtraction, *all* of the values need to match the reference value? And the error message suggests that you should either use `.any` or `.all` on the comparison result - I wonder which is appropriate, again, given that you want *all* of the values to match? – Karl Knechtel Jul 17 '20 at 08:26

2 Answers2

1

You get a ValueError because the use of == here returns an array of bools representing the equality of each element, not a bool itself. For example:

np.array([1, 2]) == [1, 2]

returns np.array([True, True]), not True. To get the result as a single boolean, use all(), which will return True if all elements in the bool array it is called on are True. The fixed if condition becomes:

((np.array(e[i][j+1]) - np.array(e[i][j])) == [0,1]).all()

With the fixed condition, the code ends up outputting:

([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])

which seems right.

Mario Ishac
  • 5,060
  • 3
  • 21
  • 52
0

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.

Cano707
  • 181
  • 1
  • 10