turn a dictionnary into a 2 dimensional list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spykSpygel
    New Member
    • Oct 2009
    • 3

    turn a dictionnary into a 2 dimensional list

    Hi, i am very new to python. I got a dictionnary which i want to format in order to use it as a two dimensional list.

    Here is an exemple of my data:
    Code:
    {(1, 3): 0.040000000000000001, (3, 0): 0.040000000000000001, 
    (2, 1): 0.050000000000000003, (0, 3): 0.050000000000000003, 
    (4, 0): 0.02, (1, 2): 0.053333333333333337, (3, 3): 0.02, 
    (4, 4): 0.0066666666666666671, (2, 2): 0.040000000000000001,
    (4, 1): 0.016666666666666666, (1, 1): 0.066666666666666666, 
    (3, 2): 0.026666666666666668, (0, 0): 0.10000000000000001, 
    (0, 4): 0.033333333333333333, (1, 4): 0.026666666666666668, 
    (2, 3): 0.029999999999999999, (4, 2): 0.013333333333333334, 
    (1, 0): 0.080000000000000002, (0, 1): 0.083333333333333329, 
    (3, 1): 0.033333333333333333, (2, 4): 0.02, 
    (2, 0): 0.059999999999999998, (4, 3): 0.01, 
    (3, 4): 0.013333333333333334, (0, 2): 0.066666666666666666}


    In fact it is just a representation of a table of data with keys as (line, column)
    so i want to get something like that:
    line[0] = ["0.100000000000 00001","0.08333 3333333333329", ]
    line[1] = ["<value of (1,0)>", "<value of (1,1)>", .....] and so forth

    I tried every possible way i know to do that in python but i don't know to much about python.

    Appreciate any help or advice
    Thanx
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    If you were to encapsulate you 2D data in a class object, you could define methods to display your formatted data. Example:
    Code:
    dd = {(1, 3): 0.040000000000000001,
          (3, 0): 0.040000000000000001,
          (2, 1): 0.050000000000000003,
          (0, 3): 0.050000000000000003,
          (4, 0): 0.02,
          (1, 2): 0.053333333333333337,
          (3, 3): 0.02,
          (4, 4): 0.0066666666666666671,
          (2, 2): 0.040000000000000001,
          #(4, 1): 0.016666666666666666,
          (1, 1): 0.066666666666666666,
          (3, 2): 0.026666666666666668,
          (0, 0): 0.10000000000000001,
          (0, 4): 0.033333333333333333,
          (1, 4): 0.026666666666666668,
          #(2, 3): 0.029999999999999999,
          (4, 2): 0.013333333333333334,
          (1, 0): 0.080000000000000002,
          (0, 1): 0.083333333333333329,
          (3, 1): 0.033333333333333333,
          (2, 4): 0.02,
          (2, 0): 0.059999999999999998,
          (4, 3): 0.01,
          (3, 4): 0.013333333333333334,
          (0, 2): 0.066666666666666666}
    
    
    class TwoDList(object):
        def __init__(self, dd):
            keys = dd.keys()
            keys.sort()
            self.arraydims = keys[-1][0]+1, keys[-1][-1]+1
            self.dd = dd
            self.array = [[None for i in range(self.arraydims[1])] \
                          for j in range(self.arraydims[0])]
                          
            for i in range(self.arraydims[0]):
                for j in range(self.arraydims[1]):
                    self.array[i][j] = dd.get((i,j), 0.0)
                    
        def __iter__(self):
            for item in self.array:
                yield item
            
        def __str__(self):
            return "\n".join([" ".join(["%0.4f" % (n) \
                               for n in item]) \
                                   for item in self])
    
    print
    x = TwoDList(dd)
    print x
    Output:
    Code:
    >>> 
    0.1000 0.0833 0.0667 0.0500 0.0333
    0.0800 0.0667 0.0533 0.0400 0.0267
    0.0600 0.0500 0.0400 0.0000 0.0200
    0.0400 0.0333 0.0267 0.0200 0.0133
    0.0200 0.0000 0.0133 0.0100 0.0067
    >>>
    Otherwise, if you know the dimensions of the array, you can print the contents directly as in:
    Code:
    dd = {(1, 3): 0.040000000000000001,
          (3, 0): 0.040000000000000001,
          (2, 1): 0.050000000000000003,
          (0, 3): 0.050000000000000003,
          (4, 0): 0.02,
          (1, 2): 0.053333333333333337,
          (3, 3): 0.02,
          (4, 4): 0.0066666666666666671,
          (2, 2): 0.040000000000000001,
          (4, 1): 0.016666666666666666,
          (1, 1): 0.066666666666666666,
          (3, 2): 0.026666666666666668,
          (0, 0): 0.10000000000000001,
          (0, 4): 0.033333333333333333,
          (1, 4): 0.026666666666666668,
          (2, 3): 0.029999999999999999,
          (4, 2): 0.013333333333333334,
          (1, 0): 0.080000000000000002,
          (0, 1): 0.083333333333333329,
          (3, 1): 0.033333333333333333,
          (2, 4): 0.02,
          (2, 0): 0.059999999999999998,
          (4, 3): 0.01,
          (3, 4): 0.013333333333333334,
          (0, 2): 0.066666666666666666}
    
    arraydims = 5,5
    print
    print "\n".join([" ".join(["%0.4f" % (dd[(j,i)]) \
                               for i in range(arraydims[1])]) \
                                   for j in range(arraydims[0])])

    Comment

    • spykSpygel
      New Member
      • Oct 2009
      • 3

      #3
      Thanx very much for you reply the second solution is easier to me , but the first one using the Object representation of data is very interesting, i do not know a lot about OOP but it seems very powefull in some situations.

      Comment

      Working...