Filter values in a list using an array with boolean expressions

2024/5/20 20:06:53

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)]
Answer

Python list object, unlike Numpy array, doesn't support boolean indexing directly. For that you could use itertools.compress function:

>>> from itertools import compress
>>> list(compress(listOfTuples,bool_array))
[(0, 1), (3, 1)]

Note that one of the advantages of compress along side its functional structure which can be very useful in many cases, is that it returns a generator and its very memory efficient in cases where you have a very large list object to filter.

If you want you can also to loop over the result if you wish to process the items one by one instead of converting the whole object to a list:

for item in compress(listOfTuples,bool_array):#do stuff
https://en.xdnf.cn/q/72840.html

Related Q&A

Show two correlation coefficients on pairgrid plot with hue (categorical variable) - seaborn python

I found a function to compute a correlation coefficient and then add it to a pair plot (shown below). My issue is that when I run a pairplot with hue (a categorical variable) the correlation coefficien…

How to properly setup vscode with pyside? Missing suggestions

Im very new to pyside, qt and python. I managed to setup a project with a basic window and a push button which closes the app. My problem is, that somehow vscode wont show all properties available, eve…

Split marker and line in Legend - Matplotlib

I want to make a legend where I specify the value for the markers and the value for the lines but not the combination of both.This example should help to illustrate my goal:import matplotlib.pyplot as …

How do I loop over all items in a DynamoDB table using boto?

Id like to query a DynamoDB table and retrieve all the items and loop over them using boto. How do I structure a query or scan that returns everything in the table?

install pyopencv with pip on Mac OS X

I am trying to install pyopencv with pip in OS X Mountain Lion and it fails by import setuptools. Following is my work. what is "Library" in setuptools? I have not seen that before. I alread…

OpenCV remap interpolation error?

Im using opencv remap function to map an image to another coordinate system. However, my initial tests indicate that there are some issues with the interpolation. Here, I give a simple example of a co…

Installing python with python win32 extensions on a network drive

I need to keep a large number of Windows XP machines running the same version of python, with an assortment of modules, one of which is python-win32. I thought about installing python on a network dri…

Python pytest hangs. For instance, pytest --version simply hangs

The following hangs:PS C:\Users\Fowler> pytest --version Notes:I am in Windows 10. By hang, I mean at least 5 minutes of waiting for the pytest --version to return... While waiting for pytest, pyth…

PyQt4 signals and slots

I am writing my first Python app with PyQt4. I have a MainWindow and a Dialog class, which is a part of MainWindow class:self.loginDialog = LoginDialog();I use slots and signals. Heres a connection mad…

Masking a pandas DataFrame with a numpy array vs DataFrame

I want to use a 2D boolean mask to selectively alter some cells in a pandas DataFrame. I noticed that I cannot use a numpy array (successfully) as the mask, but I can use a DataFrame. More frustratin…