'Regular Expressions: Search in list
I want to filter strings in a list based on a regular expression.
Is there something better than [x for x in list if r.match(x)] ?
Solution 1:[1]
You can create an iterator in Python 3.x or a list in Python 2.x by using:
filter(r.match, list)
To convert the Python 3.x iterator to a list, simply cast it; list(filter(..)).
Solution 2:[2]
Full Example (Python 3):
For Python 2.x look into Note below
import re
mylist = ["dog", "cat", "wildcat", "thundercat", "cow", "hooo"]
r = re.compile(".*cat")
newlist = list(filter(r.match, mylist)) # Read Note below
print(newlist)
Prints:
['cat', 'wildcat', 'thundercat']
Note:
For Python 2.x developers, filter returns a list already. In Python 3.x filter was changed to return an iterator so it has to be converted to list (in order to see it printed out nicely).
Solution 3:[3]
To do so without compiling the Regex first, use a lambda function - for example:
from re import match
values = ['123', '234', 'foobar']
filtered_values = list(filter(lambda v: match('^\d+$', v), values))
print(filtered_values)
Returns:
['123', '234']
filter() just takes a callable as it's first argument, and returns a list where that callable returned a 'truthy' value.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ma0 |
| Solution 2 | |
| Solution 3 | Collin Heist |
