4

I wish to turn the following dictionary into a matrix where the first and second values of the dictionary are the column and row values. Where the matrix is true I want there to be a '1' and when it is false I want a '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 output would 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
  • Sorry, I don't understand the format of your dictionary; could you explain a bit more how you're getting from there to the desired output? What does "first and second values of the dictionary" refer to? Commented Aug 18, 2012 at 3:57
  • 1
    Where the matrix is true? That doesn't make any sense. Can you please explain what constitutes true and false for each element? Commented Aug 18, 2012 at 4:05
  • @Aesthete, I believe the idea is that an integrer value 1 is True and 0 is False. Though, your question is legitimate considering the vagueness of the question (and lack of full explanation of the input -> output relationship. Commented Aug 18, 2012 at 4:33
  • It's confusing.. Is 0: [2, 5.0], supposed to be 0: [2, 5, 0], where the last value specifyies True or False? Why is one matrix index an int and the other a float? Commented Aug 18, 2012 at 4:39

3 Answers 3

2

Here is one that matches your desired output, though I think using @Antimony's answer as a basis (numpy) is likely the way to go (at least more-so than this 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 to @Antimony's answer, here is one using numpy:

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
Sign up to request clarification or add additional context in comments.

Comments

0

Your given desired output makes no sense, but here's something that does what I'm guessing you actually want. You can trim off 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]])

1 Comment

I think the desired output does make sense, even though the description is very unclear. I think it's equivalent to m = numpy.zeros((8,8)); for i,j in d.itervalues(): m[i-1,j-1] = 1; m[j-1,i-1] = 1.
0

Using numbers as dictionary keys doesn't make any 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 produce a square matrix for arbitrary sized lists:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.