2016-07-26 1 views
2

私はpython2.7を使用しています。私は、染色体の場所と実験IDを含むファイルを持っています。私は2つのリストに、現時点で保存され、この情報を持っている:リストに関連付けられた辞書を作成し、ループでこれを更新します。

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

私はあなたが正しく

はあなたが行うことができます(場所は、辞書のキーとして使用できる場合)を理解しない場合:私はこれを実行していない

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

のTry 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']} 
関連する問題