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, because it is matching the regex twice for each > item, and it is a bit ugly.
> Does anyone have a better solution? Use a language with *real* list comprehensions? Flamebait aside, you can use another level of comprehension, i.e.: n = [m.group(1) for m in (r.match(x) for x in l) if m] -- http://mail.python.org/mailman/listinfo/python-list