在Python中获取两个列表的交集 [英] Getting intersection of two lists in python

查看:159
本文介绍了在Python中获取两个列表的交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要分析的基因有两个清单.从本质上讲,我想对这些列表中的元素进行排序的方式与Venn图大致相同,即仅将出现在列表1中的元素放在一个列表中,将仅出现在列表2中的元素放在另一个列表中,将同时出现在两个列表中的元素放在一个列表中.第三.

I have two lists of genes that i'm analyzing. Essentially I want to sort the elements of these lists much in the same way as a Venn diagram, i.e. elements that only occur in list 1 are placed in one list, those only in list 2 are in another and those occurring in both are in a third.

到目前为止,我的代码:

My code so far:

from Identify_Gene import Retrieve_Data #custom class
import argparse
import os

#enable use from command line
parser = argparse.ArgumentParser(description='''\n\nFind the intersection between two lists of genes\n ''')
parser.add_argument('filename1',help='first list of genes to compare')
parser.add_argument('filename2',help='second list of genes to compare')
parser.add_argument('--output_path',help='provide an output filename')
args = parser.parse_args()

os.chdir(args.output_path)

a = Retrieve_Data() # custom class, simply produces a python list
list1 = a.parse_gene_list(args.filename1)
list2 = a.parse_gene_list(args.filename2)

intersection = []
list1_only = []
list2_only = []
if len(list1)>len(list2):
    for i in range(0,len(list1)):
        if list1[i] in list2:
            intersection.append(list1[i])
        else:
            list1_only.append(list1[i])
    for i in range(0,len(list2)):
        if list2[i] not in list1:
            list2_only.append(list2[i])
else:
    for i in range(0,len(list2)):
        if list2[i] in list1:
            intersection.append(list2[i])
        else:
            list2_only.append(list2[i])
    for i in range(0,len(list1)):
        if list1[i] not in list2:
            list1_only.append(list2[i])




filenames = {}
filenames['filename1'] = 'list1_only.txt'
filenames['filename2'] = 'list2_only.txt'
filenames['intersection'] = 'intersection.txt'                

with open(filenames['filename1'],'w') as f:
    for i in range(0,len(list1_only)):
        f.write(list1_only[i]+'\n')

with open(filenames['filename2'],'w') as f:
    for i in range(0,len(list2_only)):
        f.write(list2_only[i]+'\n')

with open(filenames['intersection'],'w') as f:
    for i in range(0,len(intersection)):
        f.write(intersection[i]+'\n')

该程序当前为我提供了两个相同的列表,分别为list1_only和list2_only,它们应该互斥.产生的相交文件是不同的,尽管我不认为它是可以信任的,因为其他两个列表的行为不符合预期.

This program currently gives me two identical lists as list1_only and list2_only where they should be mutually exclusive. The intersection file produced is different, though i don't feel it can be trusted since the other two lists are not behaving as expected.

我已被告知(自发布此问题以来),可以通过python'Sets'模块轻松完成此操作,但是出于教育目的,我还是很想修复此程序

I have been informed (since posting this question) that this operation can easily be done via the python 'Sets' module however, for educational purposes, i'd still quite like to fix this program

推荐答案

列表的构建存在错误.

在该部分:

for i in range(0,len(list1)):
    if list1[i] not in list2:
        list1_only.append(list2[i])

最后一行应该是:

        list1_only.append(list1[i])

这篇关于在Python中获取两个列表的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆