The simple way to search for a string in a list is just to use ‘if string in list’. eg:
>>> list = ['a cat', 'a dog', 'a yacht']
>>> string = 'a cat'
>>>if string in list:
... print 'found a cat!'
... found a cat!
But what if you need to search for just ‘cat’ or some other regular expression and return a list of the list items that match, or a list of selected parts of list items that match.
Then you need list comprehension.
>>> import re
>>> list = ['a cat', 'a dog', 'a yacht', 'cats']
>>> regex = re.compile(".*(cat).")
>>> [m.group(0) for l in list for m in [regex.search(l)] if m]
['a cat', 'cats']
>>> [m.group(1) for l in list for m in [regex.search(l)] if m]
['cat', 'cat']
Lets work through that. Firstly we’re going to use the regular expression library so we import that. re.compile lets us create a regular expression that we can later use for search and matching.
Now lets look at a simpler comprehension line:
>>> [m for l in list for m in [regex.search(l)]] [<_sre.SRE_Match object at 0x10cfbb0a8>, None, None, <_sre.SRE_Match object at 0x10cfbb6c0>]
This is creating a list of the results of running regex.search() on each item in l.
Next we want to exclude the None values. So we use an if statement to only include the not None values:
>>> [m for l in list for m in [regex.search(l)] if m ]
[ <_sre.SRE_Match object at 0x10cfbb6c0>, <_sre.SRE_Match object at 0x10cfd0be8> ]
>>>
Lastly rather than returning just a list of the m’s, which are RE match objects, we get the information we want from them.
>>> [m.group(0) for l in list for m in [regex.search(l)] if m]
['a cat', 'cats']
>>> [m.group(1) for l in list for m in [regex.search(l)] if m]
['cat', 'cat']
Very useful if you have say a list of Solaris 11 package fmri’s and want to extract a set of package names based on a regular expression of their version number for instance 🙂
Thank you man! A good explanation 🙂
Another option: use filter() function
x=re.comple(‘regular_string’)
sub_list = filter(x.match, string_list)
Thanks…it was really helpful to read that explanation.
Massive thanks from me, you just saved my day!
I have a particular problem where searching the net for several months without any leads.
How can I search for strings by there styles, particularly if a string is bold.