how to convert lists of lists into array in python?

Tags: , ,



I am calculating the similarity scores for a pair of nodes in a graph, the result is a lists of lists as detailed below: example output

[[[(0, 1), 0.3666666666666667], [(0, 13), 0.3333333333333333], [(0, 20), 0.23809523809523808],
      [(0, 23), 0.30952380952380953], [(20, 23), 0.21428571428571427], [(20, 31), 0.18253968253968253],
      [(20, 45), 0.21428571428571427], [(20, 46), 0.23809523809523808], [(20, 81), 0.27142857142857146],
      [(22, 24), 0.39285714285714285], [(22, 45), 0.39285714285714285], [(24, 45), 0.2857142857142857],
      [(24, 80), 0.6428571428571428], [(25, 26), 1.0], [(27, 28), 0.5333333333333333], [(27, 29), 0.45000000000000007],
      [(29, 71), 0.375]]]

here I have each node pair similarity scores How can i put this in matrix form with each column having nodes and rows bare the similarity score? Any help will be much appreciated

here example output

enter image description here

Answer

First I will remove one external level of [ ], and one of , ,, so we can convert it to dictionary as a step to make it in a matrix.

a=[[[(0, 1), 0.3666666666666667], [(0, 13), 0.3333333333333333], [(0, 20), 0.23809523809523808], [(0, 23), 0.30952380952380953],[(20, 23), 0.21428571428571427], [(20, 31), 0.18253968253968253], [(20, 45), 0.21428571428571427], [(20, 46), 0.23809523809523808], [(20, 81), 0.27142857142857146], [(22, 24), 0.39285714285714285], [(22, 45), 0.39285714285714285], [(24, 45), 0.2857142857142857], [(24, 80), 0.6428571428571428], [(25, 26), 1.0], [(27, 28), 0.5333333333333333], [(27, 29), 0.45000000000000007], [(29, 71), 0.375]]]
a=a[0] # remove one external level of `[ ]`
b=dict(a)

so we can get the value of node (n,m) as follows:

if (n,m) in b.keys():
    print(b[n,m])
else:
    print(None) # or Zero

according to the maximum dimension of nodes, it is 30 rows and 72 columns, then to convert dictionary to the array, we take the values and put it in the matrix

import numpy as np

a = [[[(0, 1), 0.3666666666666667], [(0, 13), 0.3333333333333333], [(0, 20), 0.23809523809523808],
     [(0, 23), 0.30952380952380953], [(20, 23), 0.21428571428571427], [(20, 31), 0.18253968253968253],
     [(20, 45), 0.21428571428571427], [(20, 46), 0.23809523809523808], [(20, 81), 0.27142857142857146],
     [(22, 24), 0.39285714285714285], [(22, 45), 0.39285714285714285], [(24, 45), 0.2857142857142857],
     [(24, 80), 0.6428571428571428], [(25, 26), 1.0], [(27, 28), 0.5333333333333333], [(27, 29), 0.45000000000000007],
     [(29, 71), 0.375]]]
a=a[0] # remove one external level of `[ ]`
b = dict(a)
r = 30
c = 72
Nodes = np.zeros([r, c])
for k, values in b.items():
    Nodes[k[1]][k[0]] = values

Now, we have the matrix Nodes, which has all the values of the original list in the position (r,c). The rest of the nodes have zeros.



Source: stackoverflow