intersection of 2 list of pairs

Pierre Quentel quentel.pierre at wanadoo.fr
Mon Feb 21 07:24:57 EST 2005


Another method is to build two sets of sets, one for E1 and one for E2, 
then make the intersection of these sets

- with Python 2.3

 >>> E1=[('a','g'),('r','s')]
 >>> E2=[('g','a'),('r','q'),('f','h')]
 >>> from sets import Set,ImmutableSet
 >>> f=Set([ImmutableSet(s) for s in E1])& Set([ImmutableSet(s) for s in 
E2])
 >>> [tuple(x) for x in f]
[('a', 'g')]

- with Python 2.4

 >>> E1=[('a','g'),('r','s')]
 >>> E2=[('g','a'),('r','q'),('f','h')]
 >>> f=set([frozenset(s) for s in E1]) & set([frozenset(s) for s in E2])
 >>> [tuple(x) for x in f]
[('a', 'g')]

You must use ImmutableSet or frozenset to be able to create a set of sets

Pierre



More information about the Python-list mailing list