I have a list of strings and a list of filters (which are also strings, to be interpreted as regular expressions). I want a list of all the elements in my string list that are accepted by at least one of the filters. Ideally, I'd write
[s for s in strings if some (lambda f: re.match (f, s), filters)]
where some is defined as
def some (pred, list):
for x in list:
res = pred (x)
if res:
return res
return False
Is something like that already available in Python, or is there a more idiomatic way to do this?
Python/Lisp is an interpreted and compiled, object-oriented, high-level programming language with dynamic semantics.
Lisp programming language provides good performance when compared to Python programming language. The performance of the Python programming language is less when compared to the Lisp programming language. There are macros in the Lisp Programming language. There are no macros in the Python programming language.
LISP is more powerful, more elegant, and easier to use than Python. That assumes, of course, that your team is 4 or fewer developers, all top 5%, and all in the same office.
Python any() Function The any() function returns True if any item in an iterable are true, otherwise it returns False. If the iterable object is empty, the any() function will return False.
There is a function called any
which does roughly want you want. I think you are looking for this:
[s for s in strings if any(re.match(f, s) for f in filters)]
[s for s in strings if any(re.match (f, s) for f in filters)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With