[Tutor] Simple way to compare Two lists
Andy Cheesman
Andy.cheesman at bristol.ac.uk
Fri Aug 10 14:02:28 CEST 2007
I think you could use sets, (I asked a similar question a few days ago
re numpy arrays).
ie
Convert both list to sets
use Set intersection
convert answer to lists
HTH
Andy
Tom Fitzhenry wrote:
> On Fri, Aug 10, 2007 at 02:54:44AM -0700, Jaggo wrote:
>> Can anyone think of any better way?
>
> If SmallList and BigList are sorted (in order), there is a faster method:
>
> def IsAPartOfList(SmallList,BigList):
> for i in BigList:
> for j in SmallList:
> if i==j:
> return true
> if i>j:
> break
> return false
>
> (I'm not sure how encouraged using break statements are, so wait for a tutor to
> answer)
>
> If one list is already sorted but the other isn't, it may still be faster to
> sort the unsorted list then use the method above.
>
> If neither SmallList or BigList are sorted, it's probably best to use your
> original method, which I cannot improve.
>
More information about the Tutor
mailing list