[Setup] Best way to get the intersection of (2) groups

Raphael Ritz r.ritz at biologie.hu-berlin.de
Thu Mar 17 13:41:42 UTC 2011


On 3/17/11 2:17 PM, Denis Bitouzé wrote:
> Hi,
>
> is there a better way than loops to get the intersection of (2) groups?

Assuming you have a list of ids or objects for each group
I would cast them to a set and take the intersection.
See, e.g,
http://docs.python.org/library/stdtypes.html#set-types-set-frozenset

Something like (pseudo code)

common = set(g1.listMemberIds()) & set(g2.listMemberIds())

Or interactively (Python 2.6)
 >>> l1 = [1,2,3,4]
 >>> l2 = [3,4,5,6]
 >>> c = set(l1) & set(l2)
 >>> c
set([3, 4])

This can be applied to more than 2 groups/sets as well.

Raphael

>
> Thanks.




More information about the Setup mailing list