440,772 Members | 906 Online
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
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 Sample >>> l1 = [['id1',1,1,1,True],['id2',2,1,1,True],['id3',1,1,1,True]] >>> l2[0].append(l1) Traceback (most recent call last):   File "", line 1, in ? NameError: name 'l2' is not defined >>> l2 =[][] Traceback (  File "", line 1     l2 =[][]            ^ SyntaxError: invalid syntax >>> l2 = [] >>> l2[0].append(l1) Traceback (most recent call last):   File "", line 1, in ? IndexError: list index out of range >>>    May 1 '07 #2

 Expert Mod 2.5K+ P: 2,851 I am getting the following error when I am trying Expand|Select|Wrap|Line Numbers Sample >>> l1 = [['id1',1,1,1,True],['id2',2,1,1,True],['id3',1,1,1,True]] >>> l2[0].append(l1) Traceback (most recent call last):   File "", line 1, in ? NameError: name 'l2' is not defined >>> l2 =[][] Traceback (  File "", line 1     l2 =[][]            ^ SyntaxError: invalid syntax >>> l2 = [] >>> l2[0].append(l1) Traceback (most recent call last):   File "", line 1, in ? IndexError: list index out of range >>>    Expand|Select|Wrap|Line Numbers >>> l1 = [['id1',1,1,1,True],['id2',2,1,1,True],['id3',1,1,1,True]] >>> l2 = [[]] >>> l2[0].append(l1) >>> l2 [[[['id1', 1, 1, 1, True], ['id2', 2, 1, 1, True], ['id3', 1, 1, 1, True]]]] >>>  append() is a list method. May 1 '07 #3

 100+ P: 440 Expand|Select|Wrap|Line Numbers >>> l1 = [['id1',1,1,1,True],['id2',2,1,1,True],['id3',1,1,1,True]] >>> l2 = [[]] >>> l2[0].append(l1) >>> l2 [[[['id1', 1, 1, 1, True], ['id2', 2, 1, 1, True], ['id3', 1, 1, 1, True]]]] >>>  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

 Expert Mod 2.5K+ P: 2,851 Here's an exercise I did with a list of lists: Expand|Select|Wrap|Line Numbers class Sarray(object):     def __init__(self, cols, rows):         self.cols = cols         self.rows = rows         # initialize array and fill with zeroes         self.array = [[0 for i in range(cols)] for j in range(rows)]       def setitem(self, col, row, v):         try:             self.array[row][col] = v         except IndexError, e:             print 'Valid column/row numbers are %s/%s\n' % (range(self.cols), range(self.rows))             return None       def getitem(self, col, row):         try:             return self.array[row][col]         except IndexError, e:             print 'Valid column/row numbers are %s/%s' % (range(self.cols), range(self.rows))             return None       def getcolumn(self, col):         try:             return [item[col] for item in self.array]         except IndexError, e:             print 'Valid column numbers are %s' % (range(self.cols))             return None       def getrow(self,row):         try:             return self.array[row]         except IndexError, e:             print 'Valid row numbers are %s' % (range(self.rows))             return None       def __iter__(self):         for item in self.array:             yield item       def __repr__(self):         outStr = ""         for i, item in enumerate(self):             outStr += 'Row %s = %s\n' % (i, self.array[i])         return outStr     a = Sarray(4,5) print a a.setitem(3,4,55.75) print a a.setitem(2,3,19.1)   print a print a.getitem(3,4)   print for item in a:     print item   print x = 1.5 for item in a:     print 'item =', item     for i, val in enumerate(item):         x = x*2.0         print x         item[i] = x   print a       print a.getcolumn(4) print a.getcolumn(2) print a.getrow(4) print a.getrow(2)   print a.getitem(4,4) 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 class Sarray(object):     def __init__(self, cols, rows):         self.cols = cols         self.rows = rows         # initialize array and fill with zeroes         self.array = [[0 for i in range(cols)] for j in range(rows)]       def setitem(self, col, row, v):         try:             self.array[row][col] = v         except IndexError, e:             print 'Valid column/row numbers are %s/%s\n' % (range(self.cols), range(self.rows))             return None       def getitem(self, col, row):         try:             return self.array[row][col]         except IndexError, e:             print 'Valid column/row numbers are %s/%s' % (range(self.cols), range(self.rows))             return None       def getcolumn(self, col):         try:             return [item[col] for item in self.array]         except IndexError, e:             print 'Valid column numbers are %s' % (range(self.cols))             return None       def getrow(self,row):         try:             return self.array[row]         except IndexError, e:             print 'Valid row numbers are %s' % (range(self.rows))             return None       def __iter__(self):         for item in self.array:             yield item       def __repr__(self):         outStr = ""         for i, item in enumerate(self):             outStr += 'Row %s = %s\n' % (i, self.array[i])         return outStr     a = Sarray(4,5) print a a.setitem(3,4,55.75) print a a.setitem(2,3,19.1)   print a print a.getitem(3,4)   print for item in a:     print item   print x = 1.5 for item in a:     print 'item =', item     for i, val in enumerate(item):         x = x*2.0         print x         item[i] = x   print a       print a.getcolumn(4) print a.getcolumn(2) print a.getrow(4) print a.getrow(2)   print a.getitem(4,4) 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 Sample   Input : Say GridMat                        Col 0           Col 1                  Col 2 Row 0  [ 'ID1','1','1','1','0'], [ 'ID2','2','1','1','0'] [ 'ID3','3','1','1','0'] Row 1  [ 'ID4','1','2','1','0'], [ 'ID5','2','2','1','0'] [ 'ID6','3','2','1','0'] Row 2  [ 'ID7','1','3','1','0'], [ 'ID8','2','3','1','0'] [ 'ID9','3','3','1','0']   Output : ---------------   GridMat [0][1] gives [ 'ID2','2','1','1','0'] it is Row-0 and Col-1                         or    if I say GridMat [0],then I have to get the Row-0 elements  [ 'ID1','1','1','1','0'], [ 'ID2','2','1','1','0'] [ 'ID3','3','1','1','0']   Similarly   GridMat [2] should give the output as    Row 2  [ 'ID7','1','3','1','0'], [ 'ID8','2','3','1','0'] [ 'ID9','3','3','1','0']     -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 Sample   Input : Say GridMat                        Col 0           Col 1                  Col 2 Row 0  [ 'ID1','1','1','1','0'], [ 'ID2','2','1','1','0'] [ 'ID3','3','1','1','0'] Row 1  [ 'ID4','1','2','1','0'], [ 'ID5','2','2','1','0'] [ 'ID6','3','2','1','0'] Row 2  [ 'ID7','1','3','1','0'], [ 'ID8','2','3','1','0'] [ 'ID9','3','3','1','0']   Output : ---------------   GridMat [0][1] gives [ 'ID2','2','1','1','0'] it is Row-0 and Col-1                         or    if I say GridMat [0],then I have to get the Row-0 elements  [ 'ID1','1','1','1','0'], [ 'ID2','2','1','1','0'] [ 'ID3','3','1','1','0']   Similarly   GridMat [2] should give the output as    Row 2  [ 'ID7','1','3','1','0'], [ 'ID8','2','3','1','0'] [ 'ID9','3','3','1','0']     -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

 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

 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

 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     def addrow(self, row):         self.array.append(row)         return self.array       def addcol(self, col):         for i, item in enumerate(self):             try:                 item.append(col[i])             except:                 item.append(None)         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     def addrow(self, row):         self.array.append(row)         return self.array       def addcol(self, col):         for i, item in enumerate(self):             try:                 item.append(col[i])             except:                 item.append(None)         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