【问题标题】:I how to intersect a range of columns between two horizontal list?我如何在两个水平列表之间相交一系列列?
【发布时间】:2011-05-16 17:57:52
【问题描述】:

在我正在编写的代码中,我需要将两个水平列表相交,例如:

列表A:

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

列表B

('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

如何从 listA 中获取第 2 列和第 3 列与 listB 相交的行?

想要的结果:

name2   chr11   aaaa
name3   chr7    gtag

解决方案不仅仅是“set(listA)&set(listB)”,因为列表的列数不同

感谢您的宝贵时间!

【问题讨论】:

    标签: python list intersection


    【解决方案1】:
    set_b = set(list_b)
    result = [x for x in list_a if (x[1], x[2]) in set_b]
    

    【讨论】:

    • 然后打印我想要的结果......对于结果中的c1,c2,c3:打印c1,c2,c3......我是对的吗?
    猜你喜欢
    • 2011-08-25
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    相关资源
    最近更新 更多