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
11 3390
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 - 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 "<interactive input>", line 1, in ?
-
NameError: name 'l2' is not defined
-
>>> l2 =[][]
-
Traceback ( File "<interactive input>", line 1
-
l2 =[][]
-
^
-
SyntaxError: invalid syntax
-
>>> l2 = []
-
>>> l2[0].append(l1)
-
Traceback (most recent call last):
-
File "<interactive input>", line 1, in ?
-
IndexError: list index out of range
-
>>>
-
bvdet 2,851
Expert Mod 2GB
I am getting the following error when I am trying - 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 "<interactive input>", line 1, in ?
-
NameError: name 'l2' is not defined
-
>>> l2 =[][]
-
Traceback ( File "<interactive input>", line 1
-
l2 =[][]
-
^
-
SyntaxError: invalid syntax
-
>>> l2 = []
-
>>> l2[0].append(l1)
-
Traceback (most recent call last):
-
File "<interactive input>", line 1, in ?
-
IndexError: list index out of range
-
>>>
-
- >>> 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.
- >>> 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
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
bvdet 2,851
Expert Mod 2GB
Here's an exercise I did with a list of lists: - 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
>>>
Here's an exercise I did with a list of lists: - 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 - 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
Thanks BV,
But my requirement is I need to retrieve the list data from the double dimension array - 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.
bvdet 2,851
Expert Mod 2GB
You can add new methods to the Sarray class to do whatever you want.
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.
bvdet 2,851
Expert Mod 2GB
PSB,
Just curious - did you find a solution? These methods will add rows and columns: - 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
PSB,
Just curious - did you find a solution? These methods will add rows and columns: - 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]
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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?
|
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...
|
by: swesoc |
last post by:
Hello Friends,
How do i allocate menory for a double dimension array in 'C' for
example for char **var
|
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:
|
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...
|
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...
|
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...
|
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; //...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |