Most efficient solution?

Alex new_name at mit.edu
Mon Jul 16 17:02:12 CEST 2001


> C = {}
> for item in B:
>     C[item]=None
> 
> A = filter(lambda e, dic = C: dic.has_key(e), A)

I guess you can even do filter(C.has_key, A), which is probably a little
faster still.

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

What's n, here?  This looks linear in the length of A, and constant time
in the number of unique elements in B, to me.

Alex.



More information about the Python-list mailing list