问题
I have two list of tuples
a = [('head1','a'),('head2','b'),('head3','x'),('head4','z')]
b = [('head5','u'),('head6','w'),('head7','x'),('head8','y'),('head9','z')]
I want to take the intersection of 2nd element of each tuple for example set {a[0][0],a[0][1],a[0][2],a[0][3]}
intersection with set {b[0][0],b[0][1],b[0][2],b[0][3],b[0][4]}
from list a
and b
such that it returns the first element mapping of the tuple if intersection value exist. The resulting output should be like following:
res = [('head3','head7'),('head4','head9')]
So far I have tried this:
x = [(a[i][0],b[j][0]) for i in range(len(a)) for j in range(len(b)) if a[0][i] == b[0][j]]
but got an error IndexError: tuple index out of range
What could be the correct and fastest way of doing this ?
回答1:
You can do the following in Python 3. Create dicts from your lists, taking the intersection of keys from both dicts, fetch the corresponding values at the key:
>>> da = {k:v for v, k in a}
>>> db = {k:v for v, k in b}
>>> [(da[k], db[k]) for k in da.keys()&db.keys()]
[('head4', 'head9'), ('head3', 'head7')]
In Python 2, you can use set(da).intersection(db)
in place of da.keys()&db.keys()
.
回答2:
You can use a function with a generator:
def pairs():
a = [('head1','a'),('head2','b'),('head3','x'),('head4','z')]
b = [('head5','u'),('head6','w'),('head7','x'),('head8','y'),('head9','z')]
for val1, val2 in a:
for val3, val4 in b:
if val2 == val4:
yield (val1, val3)
print(list(pairs()))
Output:
[('head3', 'head7'), ('head4', 'head9')]
来源:https://stackoverflow.com/questions/45311959/get-intersection-from-list-of-tuples