2016-07-26 3 views
2

python2.7을 사용하고 있습니다. 염색체 위치와 실험 ID가 포함 된 파일이 있습니다. 나는이 개 목록에 순간에 저장된 정보를 가지고 :목록과 관련된 사전을 만들고 루프를 통해 업데이트하십시오.

unique_locations - containing a single value for each location 
location_exp - containing lists of [location, experiment] 

나는 사전을 사용하지 않은 이유는 여러 실험에서 발견 된 여러 위치가 있다는 것입니다 - 이것은 많은 많은 관계 즉.

각 위치가 얼마나 많은 실험에서 발견되는지 알고 싶습니다. 목록의 길이가 나도리스트에 열거 (목록) 루프를 사용하여 실패한 다르기 때문에

[ 
    [location1, [experiment1, experiment2, experiment3]], 
    [location2, [experiment2, experiment3, experiment4]] 
                  ] 

: 즉 같은 목록을 얻을. 나는 시도했다 :

다른 것 중에서. 또한 여러 실험 목록과 관련된 사전을 사용해 보았습니다. 누구든지 올바른 방향으로 나를 가리킬 수 있습니까?

+0

'location_exp'목록을 간단하게 정렬 한 다음 ['itertools.groupby'] (https://docs.python.org/2/library/itertools.html#itertools.groupby)를 사용하면됩니다. 내가 상황을 올바르게 이해했는지 확신 할 수 없다. –

답변

2

내가 제대로

당신이 할 수있는 (위치가 DICT 키로 사용할 수있는 경우) 이해 않은 경우 :이 실행하지 않은

location_experiments={} 
for location, experiment in location_exp: 
    location_experiments.setdefault(location,[]).append(experiment) 
1

, 실패하면 사과 있도록. 당신은 같은 목록의 목록 [[위치, 실험], [위치, 실험]] 다음의 말한다면 :

locationList = {} 
for item in unique_experiment: 
    location = item[0] 
    exp = item[1] 
    if location not in locationList: 
     locationList[location] = [] 
     locationList[location].append(exp) 
    else: 
     locationList[location].append(exp) 
+0

이것이 완벽하게 작동했습니다. – trouselife

2

해보십시오 defaultdict, 즉 :

from collections import defaultdict 

unique_locations = ["location1", "location2"] 
location_exp = [ 
    ("location1", "experiment1"), 
    ("location1", "experiment2"), 
    ("location1", "experiment3"), 
    ("location2", "experiment2"), 
    ("location2", "experiment3"), 
    ("location2", "experiment4") 
] 

location_experiment_dict = defaultdict(list) 
for location, exp in location_exp: 
    location_experiment_dict[location].append(exp) 

print(location_experiment_dict) 

이-인쇄 출력됩니다

를 여기
defaultdict(<type 'list'>, { 
    'location2': ['experiment2', 'experiment3', 'experiment4'], 
    'location1': ['experiment1', 'experiment2', 'experiment3'] 
}) 
1

다른 동작 예이며, 내장 사용 dictgroupbyitertools에서 :

>>> from itertools import groupby 
>>> d = {} 
>>> location_exp = [ 
    ("location1", "experiment1"), 
    ("location1", "experiment2"), 
    ("location1", "experiment3"), 
    ("location2", "experiment2"), 
    ("location2", "experiment3"), 
    ("location2", "experiment4") 
] 
>>> for k,v in groupby(location_exp, itemgetter(0)): 
     d.setdefault(k,[]) 
     d[k].extend([loc for _, loc in v]) 


[] 
[] 
>>> d 
{'location2': ['experiment2', 'experiment3', 'experiment4'], 'location1': ['experiment1', 'experiment2', 'experiment3']} 
>>> 
>>> d2 = {} 
>>> location_exp2 = [ 
    ("location1", "experiment1"), 
    ("location2", "experiment2"), 
    ("location3", "experiment3"), 
    ("location1", "experiment2"), 
    ("location2", "experiment3"), 
    ("location3", "experiment4") 
] 
>>> for k,v in groupby(location_exp2, itemgetter(0)): 
     d2.setdefault(k,[]) 
     d2[k].extend([loc for _, loc in v]) 


[] 
[] 
[] 
['experiment1'] 
['experiment2'] 
['experiment3'] 
>>> d2 
{'location2': ['experiment2', 'experiment3'], 'location1': ['experiment1', 'experiment2'], 'location3': ['experiment3', 'experiment4']} 
관련 문제