[Tutor] Comparing two lists

Michael Powe michael at trollope.org
Thu Sep 16 22:00:19 CEST 2010


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

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

> >> I have two lists.

> >> 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.)

> 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

Hello,

Totally awesome.  I actually have a dictionary, with the key being an
ini file header and the value being one of these lists of ini
settings.  With your method, I am able to loop through the dictionary,
and expunge the unwanted settings.

I knew there had to be a way to take advantage of the fact that the 'i
in s' object test acts like a substring test for strings.  

Thanks.

mp

-- 
Michael Powe		michael at trollope.org		Naugatuck CT USA

'Unless we approve your idea, it will not be permitted, it will not be
allowed.'  -- Hilary Rosen, President, RIAA
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20100916/02e2c176/attachment.pgp>


More information about the Tutor mailing list