Creating nested dictionary using python
<p>I am having a data for nodes in csv format. I want to create a dictionary for the analysis. The data I have looks like </p>

<pre><code>Init node   Term node   Capacity 
   1    2   25900.20064
   1    3   23403.47319
   2    1   25900.20064
   2    6   4958.180928
   3    1   23403.47319
   3    4   17110.52372
   3    12  23403.47319
   4    3   17110.52372
   4    5   17782.7941
</code></pre>

<p>So one node is connected to other node having some capacity. So, I want a dictionary in python which creates a data like this</p>

<pre><code>graph = {'1': {'2': 25900.20064, '3': 23403.47319},
         '2': {'1': 25900.20064, '6':4958.180928},
         '3': {'1': 23403.47319, '4'}}
</code></pre>

<p>I tried following code to do this..</p>

<pre><code>import xlrd
file_location = "C:/Users/12/Desktop/SiouxFalls_net1.xlsx"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
dict = {}
z = {}


for rows in range(sheet.nrows):
    a = sheet.cell_value(rows,0)
    dict[a] = {}
    for rows in range(sheet.nrows):
        b = sheet.cell_value(rows,1)
        c = sheet.cell_value(rows, 2)
        dict[a][b] = c
</code></pre>

<p>But I am having trouble in picking up the unique value from first column and assign other nodes data linked to it. Please help!</p>

<pre><code>print(dict)
</code></pre>

<p>Try this loop:</p>

<pre><code>sheet = workbook.sheet_by_index(0)
d = {}
for rows in range(sheet.nrows):
    a = sheet.cell_value(rows, 0)
    b = sheet.cell_value(rows, 1)
    c = sheet.cell_value(rows, 2)

    d.setdefault(a, {})[b] = c
    d.setdefault(b, {})[a] = c
</code></pre>

python dictionary hyperlink nodes