How to return matrix index

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AnneTanne
    New Member
    • Jan 2013
    • 4

    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:
    Code:
        1    2    3    4    5    6    
    1    -    1    0    0    1    29   
    2    13   -    1    0    21   0    
    3    0    0    -    1    0    1    
    4    1    17   1    -    2    0    
    5    39   1    0    0    -    14   
    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?
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    You can start by passing to your Function a 2-Dimensional Array containing the Elements of the Matrix.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      You can create a 2D array from the dictionary like this:
      Code:
      m = [[0 for _ in range(6)] for _ in range(6)]
      for key in dd:
          for subkey in dd[key]:
              m[int(key)-1][int(subkey)-1] = dd[key][subkey]
      It looks like this:
      Code:
      >>> m
      [[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]]
      >>>

      From there, you can sum each column to determine which one has the lowest sum.

      Comment

      Working...