2017-04-14 3 views
0

私のデータを操作しようとしていて、何らかの問題に直面している人もいます。私は辞書のリストのように私のデータを配置python new dict値が一致するキーの値が

まず:

data = [{'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'top', 'n' : 1, 'result' : 2.5} , {'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'top', 'n' : 2, 'result' : 3.8}, {'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'top', 'n' : 3, 'result' : 2.7}, {'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'bottom', 'n' : 1, 'result' : 34.2} , {'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'bottom', 'n' : 2, 'result' : 38.6}, {'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'bottom', 'n' : 3, 'result' : 27.3}] 

ご覧のとおり、値を変更するには、オリエンテーション、複製数N結果です。

私はこの新しい配列を取得しよう:

arrangeData = [{'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'top', n : [1,2,3], 'result' : [2.5, 3.8, 2.7]}, {'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'bottom', n : [1,2,3], 'result' : [34.2, 38.6, 27.3]}] 

あなたが推測しているように、辞書の私の本当のデータリストは、時間をいくつかの化合物を含んで、一時

私の最初の愚かな仮定をループにしました

for d in data: 
    if d[0] == 'molecule1': 
     if d[1] == 18: 
      if d[2] == 20 
      ... 

ただし、ハードコーディングと全体的に不十分です。すべてのデータは、辞書の私のリストにあるのでそう、だけでなく

for d in data: 
    for c in compound: 
     for t in time: 
      for tp in temp: 
       for o in orientation: 
        if d[0] == c: 
        ... 

愚か:再び各リストを

compound = ['molecule1', 'molecule2', 'molecule3] 
time = [18, 24] 
temp = [20, 37] 
orientation = ['top', 'bottom'] 

とループ:次に

は、私はそれぞれの値のリストを使用しようとしています値のリストを導入するのは間違った方法です。

  1. 私が代わりに辞書の各条件と結果をストックする別の形式を使用する必要があります。ここでは

    が質問ありますか?

  2. dictの値を確認し、データの新しいdict(上記のarrangeDataなど)を作成するにはどうすればよいですか?

EDITまさに私が探してどのようなことを1

おかげハイヴ!あなたはグループ変数にしたいようにそれはそう一例として与えるN 化合物時間一時オリエンテーションの組み合わせのための結果arrangeDataから

答えて

0

2つの向きの値が異なるため、このコードは機能しません。

しかし、バリエーションが多すぎる場合は、非常に良い解決策ではありません。私はむしろリストの2つのリストではなく、辞書の2つのリストを作るだろう。

n_list = [[],[]] 
result_list = [[],[]] 

for i in data: 
    if i['orientation'] == 'top': 
     n_list[0].append(i['n']) 
     result_list[0].append(i['result']) 
    elif i['orientation'] == 'bottom': 
     n_list[1].append(i['n']) 
     result_list[1].append(i['result']) 


for i in data: 
    if i['orientation'] == 'top': 
     i['n'] = n_list[0] 
     i['result'] = result_list[0] 
    elif i['orientation'] == 'top': 
     i['n'] = n_list[1] 
     i['result'] = result_list[1] 


print data 

Aはるかに短いソリューションご希望の場合:

n_list = {} 
result_list = {} 

for i in data: 
    n_list.setdefault(i['orientation'], []).append(i['n']) 
    result_list.setdefault(i['orientation'], []).append(i['result']) 

for i in data: 
    i['n'] = n_list[i['orientation']] 
    i['result'] = result_list[i['orientation']] 

出力:

[{ 
    'orientation': 'top', 
    'temp': 20, 
    'compound': 'molecule1', 
    'n': [1, 2, 3], 
    'result': [2.5, 3.8, 2.7], 
    'time': 18 
}, { 
    'orientation': 'top', 
    'temp': 20, 
    'compound': 'molecule1', 
    'n': [1, 2, 3], 
    'result': [2.5, 3.8, 2.7], 
    'time': 18 
}, { 
    'orientation': 'top', 
    'temp': 20, 
    'compound': 'molecule1', 
    'n': [1, 2, 3], 
    'result': [2.5, 3.8, 2.7], 
    'time': 18 
}, { 
    'orientation': 'bottom', 
    'temp': 20, 
    'compound': 'molecule1', 
    'n': 1, 
    'result': 34.2, 
    'time': 18 
}, { 
    'orientation': 'bottom', 
    'temp': 20, 
    'compound': 'molecule1', 
    'n': 2, 
    'result': 38.6, 
    'time': 18 
}, { 
    'orientation': 'bottom', 
    'temp': 20, 
    'compound': 'molecule1', 
    'n': 3, 
    'result': 27.3, 
    'time': 18 
}] 
0

私はコードを書くつもりはありませんが、私はこれをやる方法を説明します。私は2つのループを書くだろう。最初のキーのタプル(化合物時間TEMP向き)として用いて辞書を作成し、成長しているリストとしてN結果値として。次に、2番目のループで、そのデータ構造をarrangeDataのdicts形式のリストに変換します。

これは大きなコードベースの一部であるようですが、多少のコンテキストを共有できます。あなたの目標に到達するためのより簡単な解決策さえあるかもしれません。

0

を私は(化合物、時間によってデータのこれらの行のために、あなたがそれらをグループにしたいと仮定、テンポ、オリエンテーション)。そうでない場合は、下のコードを変更することができます。

アイデアは、キー(化合物、時間、温度、および向き)の値であり、一時的な辞書(アウト)を作成することであり、値はあなたが期待したものです。ここで

{('molecule1', 18, 20, 'bottom'): {'compound': 'molecule1', 
            'n': [1, 2, 3], 
            'orientation': 'bottom', 
            'result': [34.2, 38.6, 27.3], 
            'temp': 20, 
            'time': 18}, 
('molecule1', 18, 20, 'top'): {'compound': 'molecule1', 
           'n': [1, 2, 3], 
           'orientation': 'top', 
           'result': [2.5, 3.8, 2.7], 
           'temp': 20, 
           'time': 18}} 

コードです:

from pprint import pprint 

data = [ 
    {'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'top', 'n' : 1, 'result' : 2.5} , 
    {'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'top', 'n' : 2, 'result' : 3.8}, 
    {'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'top', 'n' : 3, 'result' : 2.7}, 
    {'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'bottom', 'n' : 1, 'result' : 34.2} , 
    {'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'bottom', 'n' : 2, 'result' : 38.6}, 
    {'compound' : 'molecule1', 'time' : 18, 'temp' : 20, 'orientation' : 'bottom', 'n' : 3, 'result' : 27.3} 
] 

out = {} 
for row in data: 
    # Group the data by these columns that are the same 
    key = (row['compound'], row['time'], row['temp'], row['orientation']) 

    # This is the first time we encounter this row of data, copy most 
    # values over and create empty lists for the 'n' and 'result' 
    # column 
    if key not in out: 
     out[key] = row.copy() 
     out[key]['n'] = [] 
     out[key]['result'] = [] 

    # Now we can append the 'n' and 'result' columns 
    out[key]['n'].append(row['n']) 
    out[key]['result'].append(row['result']) 

# After we are done, we can obtain the arranged data 
arrangeData = out.values() 
pprint(arrangeData) 
関連する問題