From: Gerald Britton <gerald.britton@gm...>  20100630 13:09:18

On Wed, Jun 30, 2010 at 8:39 AM, Gerald Britton <gerald.britton@...> wrote: > OK  the worst would be to loop over the big list for every item in > the small list. You could implement a couple of approaches and choose > one at run time depending on the size of the lists: > > Approach 1 (very small list #1): > > any(p in list_2 for p in list_1) > > This is O(n^2) in the worst case but ok if either list is really small. > > Approach 2 (larger list #1): > > any(p in set(list_2) for p in list_1) e,g, >>> any(p in [1,2,3] for p in [3,4,5]) True >>> any(p in [1,2,3] for p in [6,4,5]) False >>> or >>> any(p in [1,2,3] for p in set([3,4,5])) True >>> any(p in [1,2,3] for p in set([6,4,5])) False >>> Building the set is O(n) so you need to know the size of list_2 before selecting (at run time) the right approach > > Lookups in set() are O(1) but add O(n) to build the set for a total of > O(2n) = O(n) > > You should run some timings using the timeit module to figure out the > breakeven point between the two approaches. Also, are there other > places where you look up handles in list_2? Does the order of handles > in list_2 matter? If the answer to the first is yes and the answer to > the second is no, then consider converting list_2 to a set() when you > build it. However, if I recall correctly, the order _does_ matter. > > On Wed, Jun 30, 2010 at 2:16 AM, Rob Healey <robhealey1@...> wrote: >> Greetings All: >> >> I would like to use the any command, but I do not know how yet? Here is my >> situation: >> >> I have two loops to look through: >> 1) list of person handles  a small list >> 2) ind_list which is all of the person handles in the NarrativeWeb report >> >> If person in #1 is found in l#2, break and return True else return False >> >> the list variable for #1, is handlelist >> the list variable for #2, is _individuallist >> >> I know that the any command can be used, but if there is a better and faster >> way, please show me! I would like you to please show me the code for this? >> >> Sincerely yours, >> Rob G. Healey >> >>  >> This SF.net email is sponsored by Sprint >> What will you do first with EVO, the first 4G phone? >> Visit sprint.com/first  http://p.sf.net/sfu/sprintcomfirst >> _______________________________________________ >> Grampsdevel mailing list >> Grampsdevel@... >> https://lists.sourceforge.net/lists/listinfo/grampsdevel >> >> > > > >  > Gerald Britton >  Gerald Britton 