By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,563 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,563 IT Pros & Developers. It's quick & easy.

How to return matrix index

P: 4
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
Share this Question
Share on Google+
2 Replies

ADezii
Expert 5K+
P: 8,750
You can start by passing to your Function a 2-Dimensional Array containing the Elements of the Matrix.
Jan 3 '13 #2

bvdet
Expert Mod 2.5K+
P: 2,851
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.