Most efficient solution?

Alexandre Fayolle alf at leo.logilab.fr
Mon Jul 16 10:30:30 EDT 2001


On Mon, 16 Jul 2001 09:19:09 -0400, Jay Parlar <jparlar at home.com> wrote:
>List B consists of my "stopwords", meaning, the words I don't want included in my final version of list A. So what I need to 
>do is check every item in list A, and if it occurs in list B, then I want to remove it from the final version of A. My first thought 
>would be:
>
>for eachItem in A:
>    if eachItem in B:
>        A.remove(eachItem)
>

You may get some speedup by making B a dictionary, and using has_key() to
see if the word is there. This should get you a O(log(n)) instead of O(n) 
inside the loop. To gain further performance,  use filter to skim A. 

C = {}
for item in B:
    C[item]=None

A = filter(lambda e, dic = C: dic.has_key(e), A)

Cheers,

Alexandre Fayolle
-- 
LOGILAB, Paris (France).
http://www.logilab.com   http://www.logilab.fr  http://www.logilab.org
Narval, the first software agent available as free software (GPL).



More information about the Python-list mailing list