Creating a double dimension array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    #1

    Creating a double dimension array

    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
  • psbasha
    Contributor
    • Feb 2007
    • 440

    #2
    Originally posted by psbasha
    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

    Code:
    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
    >>>

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by psbasha
      I am getting the following error when I am trying

      Code:
      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
      >>>
      Code:
      >>> 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.

      Comment

      • psbasha
        Contributor
        • Feb 2007
        • 440

        #4
        Originally posted by bvdet
        Code:
        >>> 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

        Comment

        • psbasha
          Contributor
          • Feb 2007
          • 440

          #5
          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

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Here's an exercise I did with a list of lists:
            Code:
            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.100000000000 001, 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.100000000000 001, 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.100000000000 001, 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
            >>>

            Comment

            • psbasha
              Contributor
              • Feb 2007
              • 440

              #7
              Originally posted by bvdet
              Here's an exercise I did with a list of lists:
              Code:
              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.100000000000 001, 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.100000000000 001, 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.100000000000 001, 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

              Code:
              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

              Comment

              • psbasha
                Contributor
                • Feb 2007
                • 440

                #8
                Originally posted by psbasha
                Thanks BV,
                But my requirement is I need to retrieve the list data from the double dimension array

                Code:
                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.

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  You can add new methods to the Sarray class to do whatever you want.

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    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.

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      PSB,

                      Just curious - did you find a solution? These methods will add rows and columns:
                      Code:
                          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

                      Comment

                      • psbasha
                        Contributor
                        • Feb 2007
                        • 440

                        #12
                        Originally posted by bvdet
                        PSB,

                        Just curious - did you find a solution? These methods will add rows and columns:
                        Code:
                            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]
                        >>>

                        Comment

                        Working...