a list/re problem

Ed Keith e_d_k at yahoo.com
Fri Dec 11 15:49:42 EST 2009


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


      



More information about the Python-list mailing list