I have a list of tuples like this:
listOfTuples = [(0, 1), (0, 2), (3, 1)]
and an array that could look like this:
myArray = np.array([-2, 9, 5])
Furthermore, I have an array with Boolean expressions which I created like this:
dummyArray = np.array([0, 1, 0.6])
myBooleanArray = dummyArray < 1
myBooleanArray
therefore looks like this:
array([True, False, True], dtype=bool)
Now I would like to extract values from listOfTuples
and myArray
based on myBooleanArray
. For myArray
it is straight forward and I can just use:
myArray[myBooleanArray]
which gives me the desired output
[-2 5]
However, when I use
listOfTuples[myBooleanArray]
I receive
TypeError: only integer arrays with one element can be converted to anindex
A workaround would be to convert this list to an array first by doing:
np.array(listOfTuples)[myBooleanArray]
which yields
[[0 1][3 1]]
Is there any smarter way of doing this? My desired output would be
[(0, 1), (3, 1)]