Create a jagged/ragged array in Python using classes and methods

1.7k views Asked by At

I need to create a ragged two dimensional grid in Python which has 3 rows where the 1st row has 3 columns, 2nd row - 6 columns and 3rd row - 9 columns. All that should be done without using any packages like NumPy or anything else, only with classes and methods like in the following example. Here is an example how I create regular 2d array

class newArray ():
    def __init__(self, capacity, fill = None):
        self.item = list()
        for count in range (capacity):
            self.item.append(fill)
            
    def __str__(self):
        return str (self.item)
    
    def __len__(self):
        return len(self.item)
    
    def __iter__(self):
        return iter(self.item)
    
    def __getitem__(self, index):
        return self.item[index]
    
    def __setitem__(self, index, newItem):
        self.item[index] = newItem
        
class raggedGrid():
    
    def __init__(self, rows, cols):
        self.data = newArray(rows)
        for row in range(rows):
            self.data[row] = newArray(cols)
        
                    
    def getRows(self):
        return len(self.data)
    
    def getCols(self):
        return len(self.data[0])
    
    def __getitem__(self, index):
        return self.data[index]
    
    def __setitem__(self, index, newItem):
        self.data[index] = newItem
    
    def __str__(self):
        result = '\n'
        for row in range(self.getRows()):
            for col in range(self.getCols()):
                result += str(self.data[row][col]) + ' '
            result += '\n'
        return result

After declaring it prints out a grid

a = raggedGrid(4,4)
print(a)


None None None None 
None None None None 
None None None None 
None None None None 

I stuck and don't know from where to start with this

1

There are 1 answers

4
fountainhead On BEST ANSWER

Here is modified version of raggedGrid class:

class raggedGrid():
    
    def __init__(self, rows, *col_lengths):
        self.data = newArray(rows)
        for row in range(rows):
            self.data[row] = newArray(col_lengths[row])
        
                    
    def getRows(self):
        return len(self.data)
    
    def getCols(self, row_index=None):
        if row_index is None:
            return [len(self.data[row]) for row in self.data]
        else:
            return len(self.data[row_index])
    
    
    def __getitem__(self, index):
        return self.data[index]
    
    def __setitem__(self, index, newItem):
        self.data[index] = newItem
    
    def __str__(self):
        result = '\n'
        for row_index in range(self.getRows()):
            for col_index in range(self.getCols(row_index)):
                result += repr(self.data[row_index][col_index]) + ' '
            result += '\n'
        return result

Here's an example usage for that:

a = raggedGrid(4,2,7,16,3) # Four row, having respective lengths of 2,7,16,3
print(a)
a[1][4] = "Val at [1][4]"
a[2][13] = "Val at [2][13]"
print ("Printing a after some elements were assigned")
print (a)