list - python new dict if value matching key value inside dict -


i try manipulate data , face problem, guess of know how so.

first arrange data list of 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}] 

as see, changing values orientation, replicate number n , result.

i try new arrangement :

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

as may guess, real data list of dict contain several compound, time, temp

my first stupid supposition loop on each element :

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

but it's hard coding , totaly unefficient.

then, trying use list of each value :

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

and loop again each list :

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

stupid well, because data in list of dict, introducing list of values seems wrong way.

here questions :

  1. should use format stock each conditions , result instead of dict ?
  2. how check value of dict , create new dict of data (like arrangedata mentioned above) ?

edit 1

thanks hai vu looking !

from arrangedata give example seems want group variables n , result combination of compound, time, temp , orientation.

i'm not going write code you, explain how this. write 2 loops. first creates dictionary key tuple (compound, time, temp , orientation), , values n , result growing lists. in second loop transform data structure list of dicts format of arrangedata.

it seems part of bigger codebase, maybe can share bit more of context. there might simpler solution goal.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -