def dict_to_numpy_array2(d, mapping=None):
 import numpy
 if (mapping is None):
  s = set(d.keys())
  for (k, v) in d.items():
   s.update(v.keys())
  mapping = dict(zip(s, range(len(s))))
 n = len(mapping)
 a = numpy.zeros((n, n))
 for (k1, i) in mapping.items():
  for (k2, j) in mapping.items():
   try:
    a[(i, j)] = d[k1][k2]
   except KeyError:
    pass
 return a

def dict_to_numpy_array2 ( d , mapping = None ) : import numpy if ( mapping is None ) : s = set ( d . keys ( ) ) for ( k , v ) in d . items ( ) : s . update ( v . keys ( ) ) mapping = dict ( zip ( s , range ( len ( s ) ) ) ) n = len ( mapping ) a = numpy . zeros ( ( n , n ) ) for ( k1 , i ) in mapping . items ( ) : for ( k2 , j ) in mapping . items ( ) : try : a [ ( i , j ) ] = d [ k1 ] [ k2 ] except KeyError : pass return a
convert a dictionary of dictionaries to a 2d numpy array with optional mapping .