[英]python new dict if value matching key value inside dict
我嘗試操縱我的數據,但遇到了一些問題,我想其中有些人會知道該怎么做。
首先,我將數據整理成這樣的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}]
如您所見,更改的值是direction ,復制數字n和result 。
我試圖得到這種新的安排:
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]}]
您可能會猜到,我的dict真實數據列表包含幾個復合,時間,溫度
我的第一個愚蠢的假設是遍歷每個元素:
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
感謝Hai Vu正是我想要的!
作為示例,從arrangeData
看來,您希望將變量n和結果組合在一起,以組合化合物 , 時間 , 溫度和方向 。
我不會為您編寫代碼,而是說明我將如何執行此操作。 我會寫兩個循環。 第一個創建字典,將元組( compound , time , temp和orientation )作為鍵,並將值n作為結果 ,並將結果作為增長的列表。 然后在第二個循環中,我將把該數據結構轉換為arrangeData的字典格式列表。
似乎這是更大代碼庫的一部分,也許您可以共享更多上下文。 甚至可能會有更簡單的解決方案來實現您的目標。
由於您只能有兩個不同的方向值,因此此代碼不僅僅可以工作。
但是,在這種情況下,如果您有太多的變化,那么這不是一個很好的解決方案。 我寧願列出兩個字典,而不是列出兩個列表。
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
}]
我假設對於這些數據行,您想按(化合物,時間,溫度和方向)對它們進行分組。 如果不是這種情況,您可以在下面更改我的代碼。
這個想法是創建一個臨時字典(輸出),其關鍵字是(化合物,時間,溫度和方向)的值,並且值是您期望的值:
{('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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.