On Thu, Sep 16, 2010 at 12:59:08PM -0600, Vince Spicer wrote:
> On Thu, Sep 16, 2010 at 12:49 PM, Vince Spicer <vi...@vinces.ca> wrote:

> > On Thu, Sep 16, 2010 at 12:27 PM, Michael Powe <mich...@trollope.org>wrote:

> >> alist = ['label', 'guid']

> >> blist = ['column0label', 'column1label', 'dimension0guid',
> >> 'description', 'columnid']
> >>
> >> I want to iterate over blist and extract the items that match my
> >> substrings in alist; alternatively, throw out the items that aren't in
> >> alist (but, I've had bad experiences removing items from lists "in
> >> place," so I tend toward the "copy" motif.)

> > One solution is to use list comprehensions.

> > newlist = [x for x in blist if [a for a in alist if a in x]]

> > This works, although there may be more efficient ways to accomplish this

> On major speed up is to make a simple filter that returns as soon as a match
> is found instead of
> completing the loop every element in alist
 
> def filter_(x, against):
>     for a in against:
>         if a in x:
>             return True
>     return False
> 
> newlist = [x for x in blist if filter_(x, alist)]

Hello,

Very cool, thanks.

I've used list comprehensions before but I just couldn't get the
structure right this time, for some reason.

Thanks.

mp

-- 
Michael Powe            mich...@trollope.org            Naugatuck CT USA
"No provision in our Constitution ought to be dearer to man than that
which protects the rights of conscience against the enterprises of the
civil authority." -- Thomas Jefferson to New London Methodists,
1809. ME 16:332

Attachment: pgp9X5ry33hg8.pgp
Description: PGP signature

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to