Note that there are some explanatory texts on larger screens.

plurals
  1. POcreating keys for a dictionary from a list containing the keys
    primarykey
    data
    text
    <p>I'm trying to build a matrix that holds several values in several levels. I'm trying to generate a dictionary build up like this:</p> <pre><code>{'routername':{'channel':{'01':&lt;value&gt;,'02':&lt;value&gt;}}} </code></pre> <p>The number of keys on the highest level may vary.</p> <p>The script is generating a list of available routers and another list of available channels. I wrote a rather cumbersome function that test for a key and if it is not already there, it adds the key to the dictionary.</p> <p>So, I was wondering if there isn't an easy way to create a dictionary with empty values for the keys in list 'routers'</p> <pre><code>def AddToChart(passed_seq): try: if str(passed_seq[0]) in chart_dict: if str(passed_seq[1]) in chart_dict[passed_seq[0]]: if str(passed_seq[2]) in chart_dict[passed_seq[0]][passed_seq[1]]: if str(passed_seq[3]) in chart_dict[passed_seq[0]][passed_seq[1]][passed_seq[2]]: chart_dict[passed_seq[0]][passed_seq[1]][passed_seq[2]][passed_seq[3]].update(err_sub_dict) else: chart_dict[passed_seq[0]][passed_seq[1]][passed_seq[2]].update({passed_seq[3]:{}}) chart_dict[passed_seq[0]][passed_seq[1]][passed_seq[2]][passed_seq[3]].update(err_sub_dict) else: chart_dict[passed_seq[0]][passed_seq[1]].update({passed_seq[2]:{passed_seq[3]:{}}}) chart_dict[passed_seq[0]][passed_seq[1]][passed_seq[2]][passed_seq[3]].update(err_sub_dict) else: chart_dict[passed_seq[0]].update({passed_seq[1]:{passed_seq[2]:{passed_seq[3]:{}}}}) chart_dict[passed_seq[0]][passed_seq[1]][passed_seq[2]][passed_seq[3]].update(err_sub_dict) else: chart_dict.update({passed_seq[0]:{passed_seq[1]:{passed_seq[2]:{passed_seq[3]:{}}}}}) chart_dict[passed_seq[0]][passed_seq[1]][passed_seq[2]][passed_seq[3]].update(err_sub_dict) except ValueError: print "AddToChart: ",err_sub_dict,sys.exc_info()[1][0] except: print sys.exc_info() print "AddToChart: variable not defined: " + str(passed_seq) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload