470,596 Members | 1,430 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

How to return matrix index

I have a dictionary which is made pretty as a matrix:

Dictionary:
{'1': {'3': 0, '2': 1, '5': 1, '4': 0, '6': 29},
'3': {'1': 0, '2': 0, '5': 0, '4': 1, '6': 1},
'2': {'1': 13, '3': 1, '5': 21, '4': 0, '6': 0},
'5': {'1': 39, '3': 0, '2': 1, '4': 0, '6': 14},
'4': {'1': 1, '3': 1, '2': 17, '5': 2, '6': 0},
'6': {'1': 0, '3': 43, '2': 0, '5': 0, '4': 1}}

The information as a matrix:
Expand|Select|Wrap|Line Numbers
  1.     1    2    3    4    5    6    
  2. 1    -    1    0    0    1    29   
  3. 2    13   -    1    0    21   0    
  4. 3    0    0    -    1    0    1    
  5. 4    1    17   1    -    2    0    
  6. 5    39   1    0    0    -    14   
  7. 6    0    0    43   1    0    -   
From the matrix i can see that the column number 4 has the lowest numbers.
I want to write a function findFristFread(overlaps) that takes the dictionary as argument and returns '4' because that is the column with lowest numbers.

How do i get the function to 'understand' that i want the lowest numbers?

Can anybody get me started?
Jan 3 '13 #1
2 1953
ADezii
8,801 Expert 8TB
You can start by passing to your Function a 2-Dimensional Array containing the Elements of the Matrix.
Jan 3 '13 #2
bvdet
2,851 Expert Mod 2GB
You can create a 2D array from the dictionary like this:
Expand|Select|Wrap|Line Numbers
  1. m = [[0 for _ in range(6)] for _ in range(6)]
  2. for key in dd:
  3.     for subkey in dd[key]:
  4.         m[int(key)-1][int(subkey)-1] = dd[key][subkey]
It looks like this:
Expand|Select|Wrap|Line Numbers
  1. >>> m
  2. [[0, 1, 0, 0, 1, 29], [13, 0, 1, 0, 21, 0], [0, 0, 0, 1, 0, 1], [1, 17, 1, 0, 2, 0], [39, 1, 0, 0, 0, 14], [0, 0, 43, 1, 0, 0]]
  3. >>>

From there, you can sum each column to determine which one has the lowest sum.
Jan 3 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by kosh | last post: by
3 posts views Thread by Tim Wesson | last post: by
6 posts views Thread by Chris Buckett | last post: by
2 posts views Thread by DarrenWeber | last post: by
reply views Thread by DarrenWeber | last post: by
3 posts views Thread by robert | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.