## Re: [Gramps-devel] looping through two lists, and breaking once found?

 Re: [Gramps-devel] looping through two lists, and breaking once found? From: Gerald Britton - 2010-06-30 13:09:18 ```On Wed, Jun 30, 2010 at 8:39 AM, 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 > break-even 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 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/sprint-com-first >> _______________________________________________ >> Gramps-devel mailing list >> Gramps-devel@... >> https://lists.sourceforge.net/lists/listinfo/gramps-devel >> >> > > > > -- > Gerald Britton > -- Gerald Britton ```

 [Gramps-devel] looping through two lists, and breaking once found? From: Rob Healey - 2010-06-30 06:16:07 Attachments: Message as HTML ```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 ```
 Re: [Gramps-devel] looping through two lists, and breaking once found? From: Jon Clements - 2010-06-30 12:52:36 Attachments: Message as HTML ```Hi Rob, On 30 June 2010 07:16, Rob Healey 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 > > > > Could you not just use sets? eg: overlap = set(handlelist) & set(_individuallist) That'd give you the actual handles that are in *both*. If you really want a true/false value, then you can do something like: bool( set(handlelist) & set(_individualist) ) Using any would be something like: result = any( handle in handlelist for handle in _individuallist ) Again, converting handlelist into a set would be potentially be good for performance. hth Jon. ```
 Re: [Gramps-devel] looping through two lists, and breaking once found? From: Gerald Britton - 2010-06-30 13:09:18 ```On Wed, Jun 30, 2010 at 8:39 AM, 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 > break-even 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 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/sprint-com-first >> _______________________________________________ >> Gramps-devel mailing list >> Gramps-devel@... >> https://lists.sourceforge.net/lists/listinfo/gramps-devel >> >> > > > > -- > Gerald Britton > -- Gerald Britton ```