List Comprehensions and Conditions?

2024/10/12 9:24:58

I am trying to see if I can make this code better using list comprehensions.
Lets say that I have the following lists:

a_list = ['HELLO','FOO','FO1BAR','ROOBAR','SHOEBAR']regex_list =   [lambda x: re.search(r'FOO', x, re.IGNORECASE),lambda x: re.search(r'RO', x, re.IGNORECASE)]

I basically want to add all the elements that do not have any matches in the regex_list into another list.

E.g. ==>

newlist = []
for each in a_list:for regex in regex_list:if(regex(each) == None):newlist.append(each)

How can I do this using list comprehensions? Is it even possible?

Answer

Sure, I think this should do it

newlist = [s for s in a_list if not any(r(s) for r in regex_list)]

EDIT: on closer inspection, I notice that your example code actually adds to the new list each string in a_list that doesn't match all the regexes - and what's more, it adds each string once for each regex that it doesn't match. My list comprehension does what I think you meant, which is add only one copy of each string that doesn't match any of the regexes.

https://en.xdnf.cn/q/69668.html

Related Q&A

Python in operator time complexity on range()

I have the following function:def foo(length, num):return num in range(length)Whats the time complexity of this function? Noting that range() creates a Range object on Python 3, will the time complexi…

Pandas read data from a secure FTP server in Python 3

I am looking for a neat solution to read data (using either read_csv or read_sas) to a Pandas Dataframe from a secure FTP server in Python 3. All the examples I can find are many lines and some for Pyt…

How to read XML header in Python

How can I read the header of an XML document in Python 3?Ideally, I would use the defusedxml module as the documentation states that its safer, but at this point (after hours of trying to figure this …

Shift interpolation does not give expected behaviour

When using scipy.ndimage.interpolation.shift to shift a numpy data array along one axis with periodic boundary treatment (mode = wrap), I get an unexpected behavior. The routine tries to force the firs…

HEX decoding in Python 3.2

In Python 2.x Im able to do this:>>> 4f6c6567.decode(hex_codec) OlegBut in Python 3.2 I encounter this error:>>> b4f6c6567.decode(hex_codec) Traceback (most recent call last):File &qu…

How do I access session data in Jinja2 templates (Bottle framework on app engine)?

Im running the micro framework Bottle on Google App Engine. Im using Jinja2 for my templates. And Im using Beaker to handle the sessions. Im still a pretty big Python newbie and am pretty stoked I g…

What is a dimensional range of [-1,0] in Pytorch?

So Im struggling to understand some terminology about collections in Pytorch. I keep running into the same kinds of errors about the range of my tensors being incorrect, and when I try to Google for a …

Pyinstaller executable keeps opening

Background I am working on face recognition following this link and I would like to build a standalone application using Python. My main.py script looks like the following. # main.py# Import packages a…

Occasional deadlock in multiprocessing.Pool

I have N independent tasks that are executed in a multiprocessing.Pool of size os.cpu_count() (8 in my case), with maxtasksperchild=1 (i.e. a fresh worker process is created for each new task). The mai…

How to kill a subprocess called using subprocess.call in python? [duplicate]

This question already has answers here:How to terminate a python subprocess launched with shell=True(11 answers)Closed 7 years ago.I am calling a command using subprocess.call(cmd, shell=True)The comma…