나는 분석 할 유전자 목록이 두 가지있다. 본질적으로 나는이 목록의 요소를 Venn 다이어그램과 같은 방식으로 정렬하려고합니다. 즉, 목록 1에서만 발생하는 요소는 하나의 목록에 배치되고 목록 2의 요소는 다른 요소에 포함되며 두 요소 모두에서 발생하는 요소는 제삼. 지금까지파이썬에서 두리스트의 교차 부분을 얻는다.
내 코드 :
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가 상호 배타적이 있어야 할 곳에. 생성 된 교차 파일은 다르지만 다른 두 목록이 예상대로 작동하지 않기 때문에 신뢰할 수 있다고 생각하지는 않습니다.
나는이 작업이 쉽게 교육 목적을 위해, 그러나 파이썬 '설정'모듈을 통해 수행 할 수 있음 (이 질문을 게시 이후) 통보 한 나는 여전히 매우
은 사용 해봤 세트 ? – Greg
파이썬 모듈입니까? – CiaranWelsh
['set()'] (https://docs.python.org/2/library/functions.html#func-set)은 내장되어 있습니다. 모든 항목을 해시 할 수 있다면 좋은 방법입니다. –