473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. {(1, 3): 0.040000000000000001, (3, 0): 0.040000000000000001, 
  2. (2, 1): 0.050000000000000003, (0, 3): 0.050000000000000003, 
  3. (4, 0): 0.02, (1, 2): 0.053333333333333337, (3, 3): 0.02, 
  4. (4, 4): 0.0066666666666666671, (2, 2): 0.040000000000000001,
  5. (4, 1): 0.016666666666666666, (1, 1): 0.066666666666666666, 
  6. (3, 2): 0.026666666666666668, (0, 0): 0.10000000000000001, 
  7. (0, 4): 0.033333333333333333, (1, 4): 0.026666666666666668, 
  8. (2, 3): 0.029999999999999999, (4, 2): 0.013333333333333334, 
  9. (1, 0): 0.080000000000000002, (0, 1): 0.083333333333333329, 
  10. (3, 1): 0.033333333333333333, (2, 4): 0.02, 
  11. (2, 0): 0.059999999999999998, (4, 3): 0.01, 
  12. (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.10000000000000001","0.083333333333333329", ]
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
Mar 10 '10 #1

✓ answered by bvdet

If you were to encapsulate you 2D data in a class object, you could define methods to display your formatted data. Example:
Expand|Select|Wrap|Line Numbers
  1. dd = {(1, 3): 0.040000000000000001,
  2.       (3, 0): 0.040000000000000001,
  3.       (2, 1): 0.050000000000000003,
  4.       (0, 3): 0.050000000000000003,
  5.       (4, 0): 0.02,
  6.       (1, 2): 0.053333333333333337,
  7.       (3, 3): 0.02,
  8.       (4, 4): 0.0066666666666666671,
  9.       (2, 2): 0.040000000000000001,
  10.       #(4, 1): 0.016666666666666666,
  11.       (1, 1): 0.066666666666666666,
  12.       (3, 2): 0.026666666666666668,
  13.       (0, 0): 0.10000000000000001,
  14.       (0, 4): 0.033333333333333333,
  15.       (1, 4): 0.026666666666666668,
  16.       #(2, 3): 0.029999999999999999,
  17.       (4, 2): 0.013333333333333334,
  18.       (1, 0): 0.080000000000000002,
  19.       (0, 1): 0.083333333333333329,
  20.       (3, 1): 0.033333333333333333,
  21.       (2, 4): 0.02,
  22.       (2, 0): 0.059999999999999998,
  23.       (4, 3): 0.01,
  24.       (3, 4): 0.013333333333333334,
  25.       (0, 2): 0.066666666666666666}
  26.  
  27.  
  28. class TwoDList(object):
  29.     def __init__(self, dd):
  30.         keys = dd.keys()
  31.         keys.sort()
  32.         self.arraydims = keys[-1][0]+1, keys[-1][-1]+1
  33.         self.dd = dd
  34.         self.array = [[None for i in range(self.arraydims[1])] \
  35.                       for j in range(self.arraydims[0])]
  36.  
  37.         for i in range(self.arraydims[0]):
  38.             for j in range(self.arraydims[1]):
  39.                 self.array[i][j] = dd.get((i,j), 0.0)
  40.  
  41.     def __iter__(self):
  42.         for item in self.array:
  43.             yield item
  44.  
  45.     def __str__(self):
  46.         return "\n".join([" ".join(["%0.4f" % (n) \
  47.                            for n in item]) \
  48.                                for item in self])
  49.  
  50. print
  51. x = TwoDList(dd)
  52. print x
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2. 0.1000 0.0833 0.0667 0.0500 0.0333
  3. 0.0800 0.0667 0.0533 0.0400 0.0267
  4. 0.0600 0.0500 0.0400 0.0000 0.0200
  5. 0.0400 0.0333 0.0267 0.0200 0.0133
  6. 0.0200 0.0000 0.0133 0.0100 0.0067
  7. >>> 
Otherwise, if you know the dimensions of the array, you can print the contents directly as in:
Expand|Select|Wrap|Line Numbers
  1. dd = {(1, 3): 0.040000000000000001,
  2.       (3, 0): 0.040000000000000001,
  3.       (2, 1): 0.050000000000000003,
  4.       (0, 3): 0.050000000000000003,
  5.       (4, 0): 0.02,
  6.       (1, 2): 0.053333333333333337,
  7.       (3, 3): 0.02,
  8.       (4, 4): 0.0066666666666666671,
  9.       (2, 2): 0.040000000000000001,
  10.       (4, 1): 0.016666666666666666,
  11.       (1, 1): 0.066666666666666666,
  12.       (3, 2): 0.026666666666666668,
  13.       (0, 0): 0.10000000000000001,
  14.       (0, 4): 0.033333333333333333,
  15.       (1, 4): 0.026666666666666668,
  16.       (2, 3): 0.029999999999999999,
  17.       (4, 2): 0.013333333333333334,
  18.       (1, 0): 0.080000000000000002,
  19.       (0, 1): 0.083333333333333329,
  20.       (3, 1): 0.033333333333333333,
  21.       (2, 4): 0.02,
  22.       (2, 0): 0.059999999999999998,
  23.       (4, 3): 0.01,
  24.       (3, 4): 0.013333333333333334,
  25.       (0, 2): 0.066666666666666666}
  26.  
  27. arraydims = 5,5
  28. print
  29. print "\n".join([" ".join(["%0.4f" % (dd[(j,i)]) \
  30.                            for i in range(arraydims[1])]) \
  31.                                for j in range(arraydims[0])])

2 2456
bvdet
2,851 Expert Mod 2GB
If you were to encapsulate you 2D data in a class object, you could define methods to display your formatted data. Example:
Expand|Select|Wrap|Line Numbers
  1. dd = {(1, 3): 0.040000000000000001,
  2.       (3, 0): 0.040000000000000001,
  3.       (2, 1): 0.050000000000000003,
  4.       (0, 3): 0.050000000000000003,
  5.       (4, 0): 0.02,
  6.       (1, 2): 0.053333333333333337,
  7.       (3, 3): 0.02,
  8.       (4, 4): 0.0066666666666666671,
  9.       (2, 2): 0.040000000000000001,
  10.       #(4, 1): 0.016666666666666666,
  11.       (1, 1): 0.066666666666666666,
  12.       (3, 2): 0.026666666666666668,
  13.       (0, 0): 0.10000000000000001,
  14.       (0, 4): 0.033333333333333333,
  15.       (1, 4): 0.026666666666666668,
  16.       #(2, 3): 0.029999999999999999,
  17.       (4, 2): 0.013333333333333334,
  18.       (1, 0): 0.080000000000000002,
  19.       (0, 1): 0.083333333333333329,
  20.       (3, 1): 0.033333333333333333,
  21.       (2, 4): 0.02,
  22.       (2, 0): 0.059999999999999998,
  23.       (4, 3): 0.01,
  24.       (3, 4): 0.013333333333333334,
  25.       (0, 2): 0.066666666666666666}
  26.  
  27.  
  28. class TwoDList(object):
  29.     def __init__(self, dd):
  30.         keys = dd.keys()
  31.         keys.sort()
  32.         self.arraydims = keys[-1][0]+1, keys[-1][-1]+1
  33.         self.dd = dd
  34.         self.array = [[None for i in range(self.arraydims[1])] \
  35.                       for j in range(self.arraydims[0])]
  36.  
  37.         for i in range(self.arraydims[0]):
  38.             for j in range(self.arraydims[1]):
  39.                 self.array[i][j] = dd.get((i,j), 0.0)
  40.  
  41.     def __iter__(self):
  42.         for item in self.array:
  43.             yield item
  44.  
  45.     def __str__(self):
  46.         return "\n".join([" ".join(["%0.4f" % (n) \
  47.                            for n in item]) \
  48.                                for item in self])
  49.  
  50. print
  51. x = TwoDList(dd)
  52. print x
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2. 0.1000 0.0833 0.0667 0.0500 0.0333
  3. 0.0800 0.0667 0.0533 0.0400 0.0267
  4. 0.0600 0.0500 0.0400 0.0000 0.0200
  5. 0.0400 0.0333 0.0267 0.0200 0.0133
  6. 0.0200 0.0000 0.0133 0.0100 0.0067
  7. >>> 
Otherwise, if you know the dimensions of the array, you can print the contents directly as in:
Expand|Select|Wrap|Line Numbers
  1. dd = {(1, 3): 0.040000000000000001,
  2.       (3, 0): 0.040000000000000001,
  3.       (2, 1): 0.050000000000000003,
  4.       (0, 3): 0.050000000000000003,
  5.       (4, 0): 0.02,
  6.       (1, 2): 0.053333333333333337,
  7.       (3, 3): 0.02,
  8.       (4, 4): 0.0066666666666666671,
  9.       (2, 2): 0.040000000000000001,
  10.       (4, 1): 0.016666666666666666,
  11.       (1, 1): 0.066666666666666666,
  12.       (3, 2): 0.026666666666666668,
  13.       (0, 0): 0.10000000000000001,
  14.       (0, 4): 0.033333333333333333,
  15.       (1, 4): 0.026666666666666668,
  16.       (2, 3): 0.029999999999999999,
  17.       (4, 2): 0.013333333333333334,
  18.       (1, 0): 0.080000000000000002,
  19.       (0, 1): 0.083333333333333329,
  20.       (3, 1): 0.033333333333333333,
  21.       (2, 4): 0.02,
  22.       (2, 0): 0.059999999999999998,
  23.       (4, 3): 0.01,
  24.       (3, 4): 0.013333333333333334,
  25.       (0, 2): 0.066666666666666666}
  26.  
  27. arraydims = 5,5
  28. print
  29. print "\n".join([" ".join(["%0.4f" % (dd[(j,i)]) \
  30.                            for i in range(arraydims[1])]) \
  31.                                for j in range(arraydims[0])])
Mar 10 '10 #2
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.
Mar 10 '10 #3

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

Similar topics

2
by: lawrence | last post by:
Well, www.php.net seems utterly crashed. Does anyone know how to get an XML stream and turn it into a 2 dimensional array?
7
by: Famille Delorme | last post by:
Sorry if this discussion are already submited, but I don't find anything really interresting for me. And sorry for my bad english, it is not my native language. I wrote a program in Python for a...
10
by: BCC | last post by:
Ive been googling and reading through my books but I haven't figured out a solution (much less an elegant one) to create a multidimensional array with a runtime determined number of dimensions. I...
22
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest...
3
by: acosgaya | last post by:
Hi, I would like some help as how to approach the following problem: I have a set of d-dimensional points (e.g (3,5,8,9,5) is a 5-dimensional point), and I need to sort the points on each of the...
2
by: Iwanow | last post by:
Hello! Is it possible to use ArrayList as a 2-dimensional list? I want to keep a bitmap in it, and I would like to access its elements by providing two parameters (row and column) rather than...
1
by: GB | last post by:
Hello, I have a recursion function which generate 1-dimensional vector of integers so for 3 cycles I have, for example, following vectors: int vec1 = {1,3,2,4}; int vec2 = {2,5,6,2}; int vec3 =...
6
by: fniles | last post by:
I need to store information in a 2 dimensional array. I understand ArrayList only works for a single dimensional array, is that correct ? So, I use the 2 dimensional array like in VB6. I pass the...
16
by: agent-s | last post by:
Basically I'm programming a board game and I have to use a list of lists to represent the board (a list of 8 lists with 8 elements each). I have to search the adjacent cells for existing pieces and...
3
ck9663
by: ck9663 | last post by:
Hi guys. I have a two-dimensional list. I need to see if a value is existing on the third column of each row. Just like searching a table with the third column as the index key. Do you read...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.