Re: a list/re problem

2009-12-28 Thread Steven D'Aprano
On Mon, 28 Dec 2009 17:35:22 -0800, Aahz wrote: > In article , Ed > Keith wrote: >> >>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

Re: a list/re problem

2009-12-28 Thread Aahz
In article , Ed Keith wrote: > >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 wit

Re: a list/re problem

2009-12-15 Thread Neil Cerutti
On 2009-12-11, Grant Edwards wrote: > On 2009-12-11, Neil Cerutti wrote: >> On 2009-12-11, Grant Edwards wrote: >>> [s[1:-1] for s in l if (s[0] == s[-1] == '*')] >> >> That last bit doesn't work right, does it, since an == expression >> evaluates to True or False, no the true or false value its

Re: a list/re problem

2009-12-12 Thread Nobody
On Fri, 11 Dec 2009 12:49:42 -0800, Ed Keith wrote: > 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, b

Re: a list/re problem

2009-12-12 Thread Lie Ryan
On 12/12/2009 8:24 AM, Peter Otten wrote: 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. It's

Re: a list/re problem

2009-12-12 Thread Lie Ryan
import re r = re.compile('\*(.+)\*') def f(s): m = r.match(s) if m: return m.group(1) l = ['asc', '*nbh*', 'jlsdjfdk', 'ikjh', '*jkjsdfjasd*', 'rewr'] n = [y for y in (f(x) for x in l) if y] -- http://mail.python.org/mailman/listinfo/python-list

Re: a list/re problem

2009-12-12 Thread Grant Edwards
On 2009-12-11, Neil Cerutti wrote: > On 2009-12-11, Grant Edwards wrote: >> [s[1:-1] for s in l if (s[0] == s[-1] == '*')] > > That last bit doesn't work right, does it, since an == expression > evaluates to True or False, no the true or false value itself? It works for me. Doesn't it work for

Re: a list/re problem

2009-12-12 Thread Neil Cerutti
On 2009-12-11, Grant Edwards wrote: > [s[1:-1] for s in l if (s[0] == s[-1] == '*')] That last bit doesn't work right, does it, since an == expression evaluates to True or False, no the true or false value itself? -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list

Re: a list/re problem

2009-12-11 Thread Steven D'Aprano
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

Re: a list/re problem

2009-12-11 Thread Matt Nordhoff
Grant Edwards wrote: > On 2009-12-11, 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

Re: a list/re problem

2009-12-11 Thread Ed Keith
--- On Fri, 12/11/09, Peter Otten <__pete...@web.de> wrote: > From: Peter Otten <__pete...@web.de> > Subject: Re: a list/re problem > To: python-list@python.org > Date: Friday, December 11, 2009, 4:24 PM > Ed Keith wrote: > > > I have a problem and I am tryin

Re: a list/re problem

2009-12-11 Thread Peter Otten
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

Re: a list/re problem

2009-12-11 Thread Tim Chase
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 ['

Re: a list/re problem

2009-12-11 Thread Grant Edwards
On 2009-12-11, 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

Re: a list/re problem

2009-12-11 Thread Vlastimil Brom
2009/12/11 Ed Keith : > 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

Re: a list/re problem

2009-12-11 Thread Andre Engels
On Fri, Dec 11, 2009 at 9:49 PM, 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