a list/re problem

Andre Engels andreengels at gmail.com
Fri Dec 11 16:01:06 EST 2009


On Fri, Dec 11, 2009 at 9:49 PM, Ed Keith <e_d_k at yahoo.com> wrote:
> I have a problem and I am trying to find a solution to it that is both
> efficient and elegant.
>
> I have a list call it 'l':
>
> l = ['asc', '*nbh*', 'jlsdjfdk', 'ikjh', '*jkjsdfjasd*', 'rewr']
>
> Notice that some of the items in the list start and end with an '*'. I wish to construct a new list, call it 'n' which is all the members of l that start and end with '*', with the '*'s removed.
>
> So in the case above n would be ['nbh', 'jkjsdfjasd']
>
> the following works:
>
> r = re.compile('\*(.+)\*')
>
> def f(s):
>    m = r.match(s)
>    if m:
>        return m.group(1)
>    else:
>        return ''
>
> n =  [f(x) for x in l if r.match(x)]
>
>
>
> But it is inefficient, because it is matching the regex twice for each item, and it is a bit ugly.
>
> I could use:
>
>
> n = []
> for x in keys:
>    m = r.match(x)
>        if m:
>            n.append(m.group(1))
>
>
> It is more efficient, but much uglier.
>
> Does anyone have a better solution?

Regexes seem like the proverbial sledgehammer to crack a nut here.
Note that '*' if it is present, is always 1 character, so we can
write:

n = [x[1:-1] for x in l if x.startswith("*") and x.endswith("*")]


-- 
André Engels, andreengels at gmail.com



More information about the Python-list mailing list