2016-07-26 50 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]] 
                  ] 

由於列表的長度是不同的我一直在使用上對於列的枚舉(列表)循環失敗。我曾嘗試:

location_experiment_sorted = [] 
for i, item in enumerate(unique_experiment): 
    location = item[0] 
    exp = item[1] 
    if location not in location_experiment_sorted: 
     location_experiment_sorted.append([location, exp]) 
    else: 
     location_experiment_sorted[i].append(exp) 

其中包括其他事情。我也嘗試使用與多個實驗列表相關的字典。任何人都可以將我指向正確的方向嗎?

+0

我想你可以簡單地對你的'location_exp'列表進行排序,然後使用['itertools.groupby'](https://docs.python.org/2/library/itertools.html#itertools.groupby)。不知道我是否正確理解了這種情況。 –

回答

2

如果我明白你正確 (如果位置可以作爲字典鍵)

你可以這樣做:

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']} 
相關問題