Geparada
Geparada

Reputation: 3038

I how to intersect a range of columns between two horizontal list?

At the code I'm writing I need to intersect two horizontal list like:

listA:

('name1','chr1', 'aatt')
('name2','chr11', 'aaga')
('name2','chr11', 'aaaa')
('name3','chr7', 'gtag')

listB

('chr8', 'tagt')
('chr1', 'tttt')
('chr7', 'gtag')
('chr11','aaaa')
('chr9', 'atat')

#This lists are compounded by one str per line, wich it has a "/t" in the middle. 
#Also note that are in different order

How can I get the lines from the listA whose columns 2 and 3 intersect with the listB?

desired result:

name2   chr11   aaaa
name3   chr7    gtag

The solution is not just "set(listA)&set(listB)" because the list have different number of columns

thanks for your time!

Upvotes: 0

Views: 152

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601529

set_b = set(list_b)
result = [x for x in list_a if (x[1], x[2]) in set_b]

Upvotes: 3

Related Questions