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)

break is fine! If the list you are searching is sorted you can use the 
bisect module to do a binary search instead of the linear search above.

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

I don't think BigList has to be sorted in the above algorithm. If both 
lists are sorted I suppose you could write it like a merge sort, walking 
along both lists looking for a match.

Kent
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to