Learned list comprehension today, its cool:
>>> E1=[('a','g'),('r','s')] >>> E2=[('g','a'),('r','q'),('f','h')] >>> [x for x in E1 for y in E2 if x == y or (x[1],x[0])==y] [('a', 'g')] On Sunday 20 February 2005 09:24 am, [EMAIL PROTECTED] wrote: > Hi, > I have 2 lists of tuples that look like: > E1=[('a','g'),('r','s')] and > E2=[('g','a'),('r','q'),('f','h')]. > In this tuple, the ordering does not > matter, i.e. (u,v) is the same as (v,u). > > What I want to do is the following: > given 2 list of tuples, E1 and E2, I want to create another list with > tuples that are common to both. So in the above example I would like > to return ('a','g') as being common. > So far I have the code below but it does not work out properly, I would > be grateful if someone can help me out . > thanks > > def list_of_tuples_intersect(E1,E2): > > S1=Set() > S2=Set() > for e in E1: > S1.add((e[0],e[1])) > S1.add((e[1],e[0])) > for e in E2: > S2.add((e[0],e[1])) > S2.add((e[1],e[0])) > S= S1 & S2 > SS=Set() > done=Set() > > for e in S: > if ((e[0],e[1]) in done) or ((e[1],e[0]) in done): > continue > else: > SS.add((e[0],e[1])) > done.add((e[0],e[1])) > done.add((e[1],e[0])) > return SS -- James Stroud, Ph.D. UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 -- http://mail.python.org/mailman/listinfo/python-list