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

Creating a double dimension array

100+
P: 440
Hi,

I would like to create a dimensional array ( elements should be stored in a row wise and column wise).

I have the following details in that row of each elements

Row-1-> [ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag].......[ID,X,Y,Z,Flag]
Row-2-> [ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag].......[ID,X,Y,Z,Flag]
Row-3-> [ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag].......[ID,X,Y,Z,Flag]

.................................................. ........
Row4-> [ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag].......[ID,X,Y,Z,Flag]

I have to store the elements in the order and retrieve them in order.Can anbody help me with sample code

Thanks
PSB
May 1 '07 #1
Share this Question
Share on Google+
11 Replies


100+
P: 440
Hi,

I would like to create a dimensional array ( elements should be stored in a row wise and column wise).

I have the following details in that row of each elements

Row-1-> [ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag].......[ID,X,Y,Z,Flag]
Row-2-> [ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag].......[ID,X,Y,Z,Flag]
Row-3-> [ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag].......[ID,X,Y,Z,Flag]

.................................................. ........
Row4-> [ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag],[ID,X,Y,Z,Flag].......[ID,X,Y,Z,Flag]

I have to store the elements in the order and retrieve them in order.Can anbody help me with sample code

Thanks
PSB
I am getting the following error when I am trying

Expand|Select|Wrap|Line Numbers
  1. Sample
  2. >>> l1 = [['id1',1,1,1,True],['id2',2,1,1,True],['id3',1,1,1,True]]
  3. >>> l2[0].append(l1)
  4. Traceback (most recent call last):
  5.   File "<interactive input>", line 1, in ?
  6. NameError: name 'l2' is not defined
  7. >>> l2 =[][]
  8. Traceback (  File "<interactive input>", line 1
  9.     l2 =[][]
  10.            ^
  11. SyntaxError: invalid syntax
  12. >>> l2 = []
  13. >>> l2[0].append(l1)
  14. Traceback (most recent call last):
  15.   File "<interactive input>", line 1, in ?
  16. IndexError: list index out of range
  17. >>> 
  18.  
May 1 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,851
I am getting the following error when I am trying

Expand|Select|Wrap|Line Numbers
  1. Sample
  2. >>> l1 = [['id1',1,1,1,True],['id2',2,1,1,True],['id3',1,1,1,True]]
  3. >>> l2[0].append(l1)
  4. Traceback (most recent call last):
  5.   File "<interactive input>", line 1, in ?
  6. NameError: name 'l2' is not defined
  7. >>> l2 =[][]
  8. Traceback (  File "<interactive input>", line 1
  9.     l2 =[][]
  10.            ^
  11. SyntaxError: invalid syntax
  12. >>> l2 = []
  13. >>> l2[0].append(l1)
  14. Traceback (most recent call last):
  15.   File "<interactive input>", line 1, in ?
  16. IndexError: list index out of range
  17. >>> 
  18.  
Expand|Select|Wrap|Line Numbers
  1. >>> l1 = [['id1',1,1,1,True],['id2',2,1,1,True],['id3',1,1,1,True]]
  2. >>> l2 = [[]]
  3. >>> l2[0].append(l1)
  4. >>> l2
  5. [[[['id1', 1, 1, 1, True], ['id2', 2, 1, 1, True], ['id3', 1, 1, 1, True]]]]
  6. >>> 
append() is a list method.
May 1 '07 #3

100+
P: 440
Expand|Select|Wrap|Line Numbers
  1. >>> l1 = [['id1',1,1,1,True],['id2',2,1,1,True],['id3',1,1,1,True]]
  2. >>> l2 = [[]]
  3. >>> l2[0].append(l1)
  4. >>> l2
  5. [[[['id1', 1, 1, 1, True], ['id2', 2, 1, 1, True], ['id3', 1, 1, 1, True]]]]
  6. >>> 
append() is a list method.
Thanks for the reply.

The above approach looks good ,but accessing the data from list of list seems to be complex.Is there any simple method available ,to create and access the Double dimension array values.

-PSB
May 1 '07 #4

100+
P: 440
How can I identify which is first row elemenst I am refering to ?.

Need a example for explaining the double dimension arrays using list.

-PSB
May 1 '07 #5

bvdet
Expert Mod 2.5K+
P: 2,851
Here's an exercise I did with a list of lists:
Expand|Select|Wrap|Line Numbers
  1. class Sarray(object):
  2.     def __init__(self, cols, rows):
  3.         self.cols = cols
  4.         self.rows = rows
  5.         # initialize array and fill with zeroes
  6.         self.array = [[0 for i in range(cols)] for j in range(rows)]
  7.  
  8.     def setitem(self, col, row, v):
  9.         try:
  10.             self.array[row][col] = v
  11.         except IndexError, e:
  12.             print 'Valid column/row numbers are %s/%s\n' % (range(self.cols), range(self.rows))
  13.             return None
  14.  
  15.     def getitem(self, col, row):
  16.         try:
  17.             return self.array[row][col]
  18.         except IndexError, e:
  19.             print 'Valid column/row numbers are %s/%s' % (range(self.cols), range(self.rows))
  20.             return None
  21.  
  22.     def getcolumn(self, col):
  23.         try:
  24.             return [item[col] for item in self.array]
  25.         except IndexError, e:
  26.             print 'Valid column numbers are %s' % (range(self.cols))
  27.             return None
  28.  
  29.     def getrow(self,row):
  30.         try:
  31.             return self.array[row]
  32.         except IndexError, e:
  33.             print 'Valid row numbers are %s' % (range(self.rows))
  34.             return None
  35.  
  36.     def __iter__(self):
  37.         for item in self.array:
  38.             yield item
  39.  
  40.     def __repr__(self):
  41.         outStr = ""
  42.         for i, item in enumerate(self):
  43.             outStr += 'Row %s = %s\n' % (i, self.array[i])
  44.         return outStr
  45.  
  46.  
  47. a = Sarray(4,5)
  48. print a
  49. a.setitem(3,4,55.75)
  50. print a
  51. a.setitem(2,3,19.1)
  52.  
  53. print a
  54. print a.getitem(3,4)
  55.  
  56. print
  57. for item in a:
  58.     print item
  59.  
  60. print
  61. x = 1.5
  62. for item in a:
  63.     print 'item =', item
  64.     for i, val in enumerate(item):
  65.         x = x*2.0
  66.         print x
  67.         item[i] = x
  68.  
  69. print a    
  70.  
  71. print a.getcolumn(4)
  72. print a.getcolumn(2)
  73. print a.getrow(4)
  74. print a.getrow(2)
  75.  
  76. print a.getitem(4,4)
  77. print a.getitem(3,3)
Output:>>> Row 0 = [0, 0, 0, 0]
Row 1 = [0, 0, 0, 0]
Row 2 = [0, 0, 0, 0]
Row 3 = [0, 0, 0, 0]
Row 4 = [0, 0, 0, 0]

Row 0 = [0, 0, 0, 0]
Row 1 = [0, 0, 0, 0]
Row 2 = [0, 0, 0, 0]
Row 3 = [0, 0, 0, 0]
Row 4 = [0, 0, 0, 55.75]

Row 0 = [0, 0, 0, 0]
Row 1 = [0, 0, 0, 0]
Row 2 = [0, 0, 0, 0]
Row 3 = [0, 0, 19.100000000000001, 0]
Row 4 = [0, 0, 0, 55.75]

55.75

[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 19.100000000000001, 0]
[0, 0, 0, 55.75]

item = [0, 0, 0, 0]
3.0
6.0
12.0
24.0
item = [0, 0, 0, 0]
48.0
96.0
192.0
384.0
item = [0, 0, 0, 0]
768.0
1536.0
3072.0
6144.0
item = [0, 0, 19.100000000000001, 0]
12288.0
24576.0
49152.0
98304.0
item = [0, 0, 0, 55.75]
196608.0
393216.0
786432.0
1572864.0
Row 0 = [3.0, 6.0, 12.0, 24.0]
Row 1 = [48.0, 96.0, 192.0, 384.0]
Row 2 = [768.0, 1536.0, 3072.0, 6144.0]
Row 3 = [12288.0, 24576.0, 49152.0, 98304.0]
Row 4 = [196608.0, 393216.0, 786432.0, 1572864.0]

Valid column numbers are [0, 1, 2, 3]
None
[12.0, 192.0, 3072.0, 49152.0, 786432.0]
[196608.0, 393216.0, 786432.0, 1572864.0]
[768.0, 1536.0, 3072.0, 6144.0]
Valid column/row numbers are [0, 1, 2, 3]/[0, 1, 2, 3, 4]
None
98304.0
>>>
May 1 '07 #6

100+
P: 440
Here's an exercise I did with a list of lists:
Expand|Select|Wrap|Line Numbers
  1. class Sarray(object):
  2.     def __init__(self, cols, rows):
  3.         self.cols = cols
  4.         self.rows = rows
  5.         # initialize array and fill with zeroes
  6.         self.array = [[0 for i in range(cols)] for j in range(rows)]
  7.  
  8.     def setitem(self, col, row, v):
  9.         try:
  10.             self.array[row][col] = v
  11.         except IndexError, e:
  12.             print 'Valid column/row numbers are %s/%s\n' % (range(self.cols), range(self.rows))
  13.             return None
  14.  
  15.     def getitem(self, col, row):
  16.         try:
  17.             return self.array[row][col]
  18.         except IndexError, e:
  19.             print 'Valid column/row numbers are %s/%s' % (range(self.cols), range(self.rows))
  20.             return None
  21.  
  22.     def getcolumn(self, col):
  23.         try:
  24.             return [item[col] for item in self.array]
  25.         except IndexError, e:
  26.             print 'Valid column numbers are %s' % (range(self.cols))
  27.             return None
  28.  
  29.     def getrow(self,row):
  30.         try:
  31.             return self.array[row]
  32.         except IndexError, e:
  33.             print 'Valid row numbers are %s' % (range(self.rows))
  34.             return None
  35.  
  36.     def __iter__(self):
  37.         for item in self.array:
  38.             yield item
  39.  
  40.     def __repr__(self):
  41.         outStr = ""
  42.         for i, item in enumerate(self):
  43.             outStr += 'Row %s = %s\n' % (i, self.array[i])
  44.         return outStr
  45.  
  46.  
  47. a = Sarray(4,5)
  48. print a
  49. a.setitem(3,4,55.75)
  50. print a
  51. a.setitem(2,3,19.1)
  52.  
  53. print a
  54. print a.getitem(3,4)
  55.  
  56. print
  57. for item in a:
  58.     print item
  59.  
  60. print
  61. x = 1.5
  62. for item in a:
  63.     print 'item =', item
  64.     for i, val in enumerate(item):
  65.         x = x*2.0
  66.         print x
  67.         item[i] = x
  68.  
  69. print a    
  70.  
  71. print a.getcolumn(4)
  72. print a.getcolumn(2)
  73. print a.getrow(4)
  74. print a.getrow(2)
  75.  
  76. print a.getitem(4,4)
  77. print a.getitem(3,3)
Output:>>> Row 0 = [0, 0, 0, 0]
Row 1 = [0, 0, 0, 0]
Row 2 = [0, 0, 0, 0]
Row 3 = [0, 0, 0, 0]
Row 4 = [0, 0, 0, 0]

Row 0 = [0, 0, 0, 0]
Row 1 = [0, 0, 0, 0]
Row 2 = [0, 0, 0, 0]
Row 3 = [0, 0, 0, 0]
Row 4 = [0, 0, 0, 55.75]

Row 0 = [0, 0, 0, 0]
Row 1 = [0, 0, 0, 0]
Row 2 = [0, 0, 0, 0]
Row 3 = [0, 0, 19.100000000000001, 0]
Row 4 = [0, 0, 0, 55.75]

55.75

[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 19.100000000000001, 0]
[0, 0, 0, 55.75]

item = [0, 0, 0, 0]
3.0
6.0
12.0
24.0
item = [0, 0, 0, 0]
48.0
96.0
192.0
384.0
item = [0, 0, 0, 0]
768.0
1536.0
3072.0
6144.0
item = [0, 0, 19.100000000000001, 0]
12288.0
24576.0
49152.0
98304.0
item = [0, 0, 0, 55.75]
196608.0
393216.0
786432.0
1572864.0
Row 0 = [3.0, 6.0, 12.0, 24.0]
Row 1 = [48.0, 96.0, 192.0, 384.0]
Row 2 = [768.0, 1536.0, 3072.0, 6144.0]
Row 3 = [12288.0, 24576.0, 49152.0, 98304.0]
Row 4 = [196608.0, 393216.0, 786432.0, 1572864.0]

Valid column numbers are [0, 1, 2, 3]
None
[12.0, 192.0, 3072.0, 49152.0, 786432.0]
[196608.0, 393216.0, 786432.0, 1572864.0]
[768.0, 1536.0, 3072.0, 6144.0]
Valid column/row numbers are [0, 1, 2, 3]/[0, 1, 2, 3, 4]
None
98304.0
>>>
Thanks BV,
But my requirement is I need to retrieve the list data from the double dimension array

Expand|Select|Wrap|Line Numbers
  1. Sample
  2.  
  3. Input : Say GridMat
  4.  
  5.                      Col 0           Col 1                  Col 2
  6. Row 0  [ 'ID1','1','1','1','0'], [ 'ID2','2','1','1','0'] [ 'ID3','3','1','1','0']
  7. Row 1  [ 'ID4','1','2','1','0'], [ 'ID5','2','2','1','0'] [ 'ID6','3','2','1','0']
  8. Row 2  [ 'ID7','1','3','1','0'], [ 'ID8','2','3','1','0'] [ 'ID9','3','3','1','0']
  9.  
  10. Output :
  11. ---------------
  12.  
  13. GridMat [0][1] gives [ 'ID2','2','1','1','0'] it is Row-0 and Col-1
  14.                         or 
  15.  
  16. if I say GridMat [0],then I have to get the Row-0 elements
  17.  [ 'ID1','1','1','1','0'], [ 'ID2','2','1','1','0'] [ 'ID3','3','1','1','0']
  18.  
  19. Similarly
  20.  
  21. GridMat [2] should give the output as 
  22.  
  23. Row 2  [ 'ID7','1','3','1','0'], [ 'ID8','2','3','1','0'] [ 'ID9','3','3','1','0']
  24.  
  25.  
-PSB
May 1 '07 #7

100+
P: 440
Thanks BV,
But my requirement is I need to retrieve the list data from the double dimension array

Expand|Select|Wrap|Line Numbers
  1. Sample
  2.  
  3. Input : Say GridMat
  4.  
  5.                      Col 0           Col 1                  Col 2
  6. Row 0  [ 'ID1','1','1','1','0'], [ 'ID2','2','1','1','0'] [ 'ID3','3','1','1','0']
  7. Row 1  [ 'ID4','1','2','1','0'], [ 'ID5','2','2','1','0'] [ 'ID6','3','2','1','0']
  8. Row 2  [ 'ID7','1','3','1','0'], [ 'ID8','2','3','1','0'] [ 'ID9','3','3','1','0']
  9.  
  10. Output :
  11. ---------------
  12.  
  13. GridMat [0][1] gives [ 'ID2','2','1','1','0'] it is Row-0 and Col-1
  14.                         or 
  15.  
  16. if I say GridMat [0],then I have to get the Row-0 elements
  17.  [ 'ID1','1','1','1','0'], [ 'ID2','2','1','1','0'] [ 'ID3','3','1','1','0']
  18.  
  19. Similarly
  20.  
  21. GridMat [2] should give the output as 
  22.  
  23. Row 2  [ 'ID7','1','3','1','0'], [ 'ID8','2','3','1','0'] [ 'ID9','3','3','1','0']
  24.  
  25.  
-PSB
I dont now the size of the Matrix ( Rows and Columns) .Dynamically has to insert the data,from the output as I get.
May 1 '07 #8

bvdet
Expert Mod 2.5K+
P: 2,851
You can add new methods to the Sarray class to do whatever you want.
May 2 '07 #9

bvdet
Expert Mod 2.5K+
P: 2,851
Maybe you should look into Numpy. Unfortunately, I don't have any experience with it, but it should handle your arrays well. The Sarray class was an exercise I did a few months ago.
May 2 '07 #10

bvdet
Expert Mod 2.5K+
P: 2,851
PSB,

Just curious - did you find a solution? These methods will add rows and columns:
Expand|Select|Wrap|Line Numbers
  1.     def addrow(self, row):
  2.         self.array.append(row)
  3.         return self.array
  4.  
  5.     def addcol(self, col):
  6.         for i, item in enumerate(self):
  7.             try:
  8.                 item.append(col[i])
  9.             except:
  10.                 item.append(None)
  11.         return self.array
May 3 '07 #11

100+
P: 440
PSB,

Just curious - did you find a solution? These methods will add rows and columns:
Expand|Select|Wrap|Line Numbers
  1.     def addrow(self, row):
  2.         self.array.append(row)
  3.         return self.array
  4.  
  5.     def addcol(self, col):
  6.         for i, item in enumerate(self):
  7.             try:
  8.                 item.append(col[i])
  9.             except:
  10.                 item.append(None)
  11.         return self.array
I have implemented in the following way

[code]

>>> l = [[[1,2,3],[2,1,2]],[[1,1,1],[2,1,3]]]
#First Row
>>> l[0]
[[1, 2, 3], [2, 1, 2]]
#Second Row
>>> l[1]
[[1, 1, 1], [2, 1, 3]]
#First Row,First Column

>>> l[0][0]
[1, 2, 3]
#First Row,Second Column

>>> l[0][1]
[2, 1, 2]
>>>
May 3 '07 #12

Post your reply

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