在我写的代码中,我需要交叉两个水平列表,如:
chr1 aatt
chr8 tagg
chr11 aaaa
chr7 gtag
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
如何才能获得这两个列表的交集?
chr7 gtag
chr11 aaaa
我也可以生成每行两个字符串的列表,如下所示:\
('chr1', 'aatt')
('chr8', 'tagg')
('chr11', 'aaaa')
('chr7', 'gtag')
('chr8', 'tagt')
('chr1', 'tttt')
('chr7', 'gtag')
('chr11','aaaa')
('chr9', 'atat')
这种情况下的重要事项是必须将两列视为一列
谢谢你的时间!答案 0 :(得分:5)
转换为集合并交叉:set(a) & set(b)
答案 1 :(得分:4)
使用Python sets
listA = (
('chr1', 'aatt'),
('chr8', 'tagg'),
('chr11', 'aaaa'),
('chr7', 'gtag'),
)
listB = (
('chr8', 'tagt'),
('chr1', 'tttt'),
('chr7', 'gtag'),
('chr11','aaaa'),
('chr9', 'atat'),
)
combined = set(listA).intersection(set(listB))
for c, d in combined:
print c, d
你也可以像这样使用&
:
combined = set(listA) & set(listB)
答案 2 :(得分:2)
使用set intersection。
setC = set(listA) & set(listB)
listC = list(setC) # if you really need a list
答案 3 :(得分:1)
将numpy导入为np
np.intersect_nu(list1, list2)
答案 4 :(得分:0)
也许通过不从列表创建2个集来进行性能优化,这需要散列列表中的所有项,但只创建1个集并迭代第二个列表。如果您知道哪个列表很大而哪个列表很小也可以提供帮助。
def intersect(smallList, largeList):
values = set(smallList)
intersection = []
for v in largeList:
if v in values:
intersection.append(v)
return intersection