Create a matrix from a list of values ​​in a dictionary

I want to turn the next dictionary into a matrix, where the first and second values ​​of the dictionary are the values ​​of columns and rows. If the matrix is ​​true, I want it to be β€œ1”, and when it is false, I want β€œ0”.

{0: [2, 5.0], 1: [6, 7.0], 2: [6, 8.0], 3: [5, 6.0], 4: [1, 5.0] , 5: [3, 4.0], 6: [4, 5.0]}

The desired result will look something like this:

1 2 3 4 5 6 7 8 1 0 0 0 0 1 0 0 0 2 0 0 0 0 1 0 0 0 3 0 0 0 1 0 0 0 0 4 0 0 1 0 1 0 0 0 5 1 1 0 1 0 1 0 0 6 0 0 0 0 1 0 1 1 7 0 0 0 0 0 1 0 0 8 0 0 0 0 0 1 0 0 

Thanks heaps, any pointers would be awesome!

Danielle

+4
source share
3 answers

Here is the one that matches your desired result, although I think using @Antimony's answer as a base (numpy) will most likely be a way (at least more than that answer)

 N = 8 d = {0: [2, 5.0], 1: [6, 7.0], 2: [6, 8.0], 3: [5, 6.0], 4: [1, 5.0], 5: [3, 4.0], 6: [4, 5.0]} m = [0] * (N ** 2 + 1) for x, y in d.values(): m[x + int(y - 1) * N] = 1 m[int(y) + (x - 1) * N] = 1 print " ".ljust(9), print " ".join(map(str, range(1, N + 1))) print for i in range(1, N ** 2 + 1): if i % N == 1: print "%d ".ljust(10) % (i / N + 1), val = m[i] print "%d " % val, if not i % N: print 

OUTPUT

  1 2 3 4 5 6 7 8 1 0 0 0 0 1 0 0 0 2 0 0 0 0 1 0 0 0 3 0 0 0 1 0 0 0 0 4 0 0 1 0 1 0 0 0 5 1 1 0 1 0 1 0 0 6 0 0 0 0 1 0 1 1 7 0 0 0 0 0 1 0 0 8 0 0 0 0 0 1 0 0 

Based on @DSM's comment for @Antimony's answer, numpy is used here:

 import numpy N = 8 m = numpy.zeros((N,N)) d = {0: [2, 5.0], 1: [6, 7.0], 2: [6, 8.0], 3: [5, 6.0], 4: [1, 5.0], 5: [3, 4.0], 6: [4, 5.0]} for i,j in d.itervalues(): m[i-1,j-1] = 1 m[j-1,i-1] = 1 print " ".ljust(9), print " ".join(map(str, range(1, N + 1))) print for i, line in enumerate(m.tolist()): print "%s %s" % (("%s".ljust(10) % (i+1)," ".join(map(str, map(int, line))))) 

OUTPUT

  1 2 3 4 5 6 7 8 1 0 0 0 0 1 0 0 0 2 0 0 0 0 1 0 0 0 3 0 0 0 1 0 0 0 0 4 0 0 1 0 1 0 0 0 5 1 1 0 1 0 1 0 0 6 0 0 0 0 1 0 1 1 7 0 0 0 0 0 1 0 0 8 0 0 0 0 0 1 0 0 
+1
source

Your desired result does not make sense, but here is something that does what I assume you really want. You can trim the 0th row and column by doing m = m[1:,1:]

 >>> d = {0: [2, 5.0], 1: [6, 7.0], 2: [6, 8.0], 3: [5, 6.0], 4: [1, 5.0], 5: [3, 4.0], 6: [4, 5.0]} >>> dims = [1 + int(x) for x in map(max, zip(*d.values()))] >>> m = numpy.zeros(dims, dtype=int) >>> for v in map(tuple, d.values()): m[v] = 1 >>> m array([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1]]) 
0
source

Using numbers as dictionary keys makes no sense. You should just use a list.

 vals = [True, True, False, True] # Assuming this is a list of n*n elements matrix = [] # This will be a two-dimensional array, or matrix. 

This will create a square matrix for lists of arbitrary size:

 from math import sqrt size = sqrt(len(vals)) for x in range(0, size): matrix.append(list(vals[x*size:x*size+size])) # matrix == [[True, True], [False, True]] # matrix[0][0] == True # int(matrix[0][0]) == 1 
0
source

All Articles