2017-04-14 142 views
0

我試圖操縱我的數據,我面臨一些問題,我想你們中的一些人會知道如何這樣做。python新詞典如果值匹配鍵值內部字典

首先,我安排我的數據,如字典的這個名單:

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}] 

正如你看到的,變更值方向,複製數ñ結果

我試圖讓這個新的安排:

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 
      ... 

但是它很難編碼,總的來說效率不高。

於是,我試圖用每個值的列表:再次

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

和循環每個列表:

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

愚蠢的爲好,因爲所有的數據都在我的字典的名單,所以引入價值清單似乎是一種錯誤的方式。

這裏有幾個問題:

  1. 我應該用另一種格式每個股票的條件和結果,而不是一個字典?
  2. 如何檢查dict的值並創建一個新的數據字典(如上面提到的arrangeData)?

編輯1

感謝海武那正是我尋找!

回答

0

由於您只能擁有兩個不同的方向值,因此此代碼將更有效。

但是,如果你有太多的變化,在這種情況下,這不是一個很好的解決方案。我寧願寫兩個字典列表,而不是兩個列表。

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 

一個更短的解決方案,如果你喜歡:

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

從你給arrangeData爲例好像你要組變量n表示複合時間臨時方向的組合結果

我不會爲你寫代碼,但解釋我會如何做到這一點。我會寫兩個循環。第一創建具有作爲密鑰的元組(化合物時間溫度取向)的字典,而當數值Ñ結果作爲生長列表。然後在第二個循環中,我會將該數據結構轉換爲arrangeData的字典格式列表。

看起來這是更大的代碼庫的一部分,也許你可以分享更多的上下文。可能有更簡單的解決方案來實現您的目標。

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)