Performance problem with filtering

Delaney, Timothy tdelaney at avaya.com
Wed Mar 13 23:06:09 EST 2002


> From: Delaney, Timothy [mailto:tdelaney at avaya.com]
> 
> So, the first thing I would recommend is ...
> 
> results = []
> c = {}
> 
> for entry in a:
>     c[entry] = None
> 
> for entry in b:
>     if c.has_key(entry):
>         results.append(entry)

Bah ...

if not c.has_key(entry) ...

> This means you are searching for a key in a dictionary ... a 
> very quick
> operation.
> 
> A further optimisation is to use the filter() function.
> 
> results = []
> c = {}
> 
> for entry in a:
>     c[entry] = None
> 
> results = filter(c.has_key, b)

Bah ...

results = filter(lambda k: not c.has_key(k), b)

That will slow it down a bit (using the lambda) but will give the right
result ;)

Tim Delaney




More information about the Python-list mailing list