-1
report['conditions'] += [
   {
    "name": cond,
    "biRad": br,
    "findings": 
     [
        {
         "name": f,
         **if f == 'mass':** 
           "parameters": 
           [
            {
             "name": iter_params_mass[0],
             "value": create_rep(iter_params_mass, row, f, r)[0]
            },{...}
           ]
        }
    ]
  }]

I have very strict structure for my BSON file, but instead of creating everything manually, I would like to use if statement and for loop (if possible), because I can have more than 1 pair of name-value in parameters. Any efficient way to do it? Thanks! I'm using Python 3.6.0

Carlos Gonzalez
  • 858
  • 13
  • 23
Alice Jarmusch
  • 467
  • 1
  • 7
  • 20

1 Answers1

1

sudo code for how this can be done:

if f == 'mass':
    # construct parameters with for loop maybe:
    parameters = {key: value for (key, value) in [('a','b'),('c','d')]}
    # parameters = {'a': 'b', 'c': 'd'}
else:
    parameters = {key: value for (key, value) in [('b','a'),('d','c')]}

then use the parameters to create final bson dict

report['conditions'] += [{"name": cond,
                          "biRad": br,
                          "findings": [
                              {"name": f,
                               "parameters": parameters
Vikash Singh
  • 13,213
  • 8
  • 40
  • 70
  • Great, thanks, will check it out. Also, can I use a loop inside a dict? – Alice Jarmusch Aug 15 '17 at 10:47
  • yes.. there are list comprehension processes for dictionary creation also. you can use those. https://stackoverflow.com/questions/1747817/create-a-dictionary-with-list-comprehension-in-python – Vikash Singh Aug 15 '17 at 10:49