473,322 Members | 1,522 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,322 software developers and data experts.

Creating a double dimension array

440 256MB
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 3390
psbasha
440 256MB
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
2,851 Expert Mod 2GB
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
psbasha
440 256MB
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
psbasha
440 256MB
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
2,851 Expert Mod 2GB
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
psbasha
440 256MB
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
psbasha
440 256MB
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
2,851 Expert Mod 2GB
You can add new methods to the Sarray class to do whatever you want.
May 2 '07 #9
bvdet
2,851 Expert Mod 2GB
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
2,851 Expert Mod 2GB
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
psbasha
440 256MB
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

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

Similar topics

4
by: Michael Kirchner | last post by:
Hi everybody The output of my multiple dimension array is quite confusing. Im declaring an array, store some values in it and then I save the array in a session variable. On an other page I...
5
by: ur8x | last post by:
I have a double pointer and a 2D array: int mat, **ptr; ptr = mat; mat = 3; Although mat and ptr are pointing at the same address, &ptr and &mat are not, why?
3
by: ZeroVisio | last post by:
Hi, Is it possible to create two -dimensional array using ArrayList in C#? I know you can do one-dimensional array but i dont know how to do two-dimensional. in my case my number of columns...
3
by: swesoc | last post by:
Hello Friends, How do i allocate menory for a double dimension array in 'C' for example for char **var
2
by: Nathan Sokalski | last post by:
I have a multidimensional array declared as the following: Dim guesses(14, 5) As Integer I want to assign all values in a specific dimension to another array declared as follows:
5
by: Jackson | last post by:
I have something that is stumping me. I am trying to initialize a 3 dimensional string array with the code below, but it wont compile. Can anyone explain what Im doing wrong?????????????? Im...
5
by: J.M. | last post by:
I am trying to use a software package in my program, the problem being different methods are used to store vectors. I have "my" vectors stored as vector<doublefrom the STL and need to pass a...
2
by: nitinm | last post by:
hi I want to make a program whose requirement are as following: 1) it has to create an NxN matrix after reading input (i.e. N) from a file in the main() itself. 2) it has to send the array as...
2
by: acohen | last post by:
I'll be most thankful if someone could help me with the following: 1. A double array is declared and allocated in an object's constructor class Snapshot { private: double **Coord; //...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.