이 코드는 샘플 데이터에서 빠르게 실행되지만 큰 파일을 반복 할 때 중첩 된 for 루프 때문에 천천히 실행되는 것처럼 보입니다. 기본값 명령의 항목을 반복하여 반복하는 다른 이유가 있습니까?namedtuple 및 dicts에 대해 정의 된 쌍에 대한 반복 속도가 느림
import itertools
sample_genes1={"0002":["GENE1", "GENE2", "GENE3", "GENE4"],
"0003":["GENE1", "GENE2", "GENE3", "GENE6"],
"0202":["GENE4", "GENE2", "GENE1", "GENE7"]}
def get_common_gene_pairs(genelist):
genedict={}
for k,v in sample_genes1.items():
listofpairs=[]
for i in itertools.combinations(v,2):
listofpairs.append(i)
genedict[k]=listofpairs
return genedict
from collections import namedtuple,defaultdict
def get_gene_pair_pids(genelist):
i=defaultdict(list)
d=get_common_gene_pairs(sample_genes1)
Pub_genes=namedtuple("pair", ["gene1", "gene2"])
for p_id,genepairs in d.iteritems():
for p in genepairs:
thispair=Pub_genes(p[0], p[1])
if thispair in i.keys():
i[thispair].append(p_id)
else:
i[thispair]=[p_id,]
return i
if __name__=="__main__":
get_gene_pair_pids(sample_genes1)
이와 같은 문제를 해결하기 위해 가장 먼저해야 할 일 중 하나는 코드를 프로파일하는 것입니다. # [Python 스크립트를 프로파일 링 할 수 있습니까? _] (http://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script)를 보면 얼마나 쉬운 지 알 수 있습니다. – martineau