a list/re problem
steve at REMOVE-THIS-cybersource.com.au
Sat Dec 12 02:55:51 CET 2009
On Fri, 11 Dec 2009 12:49:42 -0800, Ed Keith 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('\*(.+)\*')
Others have suggested using a list comp. Just to be different, here's a
version using filter and map.
l = ['asc', '*nbh*', 'jlsdjfdk', 'ikjh', '*jkjsdfjasd*', 'rewr']
l = map(
lambda s: s[1:-1] if s.startswith('*') and s.endswith('*') else '', l)
l = filter(None, l)
More information about the Python-list