a list/re problem

Vlastimil Brom vlastimil.brom at gmail.com
Fri Dec 11 16:02:23 EST 2009


2009/12/11 Ed Keith <e_d_k at yahoo.com>:
> 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?
>
> Thank,
>
>    -EdK
>
>
> Ed Keith
> e_d_k at yahoo.com
>
> Blog: edkeith.blogspot.com
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Hi,
maybe you could use a list comprehension or the equivalent loop just
using the string methods and slicing?

>>> lst = ['asc', '*nbh*', 'jlsdjfdk', 'ikjh', '*jkjsdfjasd*', 'rewr']
>>> [item[1:-1] for item in lst if (item.startswith("*") and item.endswith("*"))]
['nbh', 'jkjsdfjasd']
>>>


hth,
  vbr



More information about the Python-list mailing list