Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lisp's "some" in Python?

Tags:

python

lisp

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?

like image 805
Mark Probst Avatar asked Apr 29 '10 15:04

Mark Probst


People also ask

What is Lisp in Python?

Python/Lisp is an interpreted and compiled, object-oriented, high-level programming language with dynamic semantics.

Why Lisp Over Python?

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.

Is Lisp better than Python AI?

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.

What is the use of any in Python?

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.


2 Answers

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)]
like image 122
Mark Byers Avatar answered Nov 15 '22 15:11

Mark Byers


[s for s in strings if any(re.match (f, s) for f in filters)]
like image 40
Ofri Raviv Avatar answered Nov 15 '22 14:11

Ofri Raviv