[Tutor] Lists...

Andrew Wilkins toodles@yifan.net
Thu, 31 May 2001 11:58:46 +0800


> If the lists get large, this will have horrible performance because
> the "item in l2" performs a linear search.  If you can create a
> dictionary from one of the lists then the speed of the inersection
> will be improved, but time will be spent building the citionary.  (It
> is faster because the has_key method hashes the key and checks if it
> exists rather than performing a linear search on a list)

Ahh just what I was hoping not to receive =)
The thing is, the lists I'm using are terribly big. What I'm doing is
creating ranges of coordinates,
so the lists generally will vary from 100 to 1000 elements. I'll try the
dictionary method! If that's slow, do you think a C implementation would be
worth the trouble? I've never tried implementing modules in C, could be fun
=)

Thanks!

>
> This list comprehension shoud do the same thing :
>
> def intersect( l1 , l2 ) :
>     return [ item for item in l1 if item in l2 ]
>
>
> -D
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>