使用类和方法在Python中创建锯齿状/不规则数组

使用类和方法在Python中创建锯齿状/不规则数组,python,arrays,grid,ragged,Python,Arrays,Grid,Ragged,我需要用Python创建一个不规则的二维网格,它有3行,其中第一行有3列,第二行有6列,第三行有9列。所有这些都应该在不使用任何包(如NumPy)或其他任何东西的情况下完成,只使用以下示例中的类和方法。下面是一个如何创建常规二维阵列的示例 class newArray (): def __init__(self, capacity, fill = None): self.item = list() for count in range (capacity)

我需要用Python创建一个不规则的二维网格,它有3行,其中第一行有3列,第二行有6列,第三行有9列。所有这些都应该在不使用任何包(如NumPy)或其他任何东西的情况下完成,只使用以下示例中的类和方法。下面是一个如何创建常规二维阵列的示例

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
声明后,将打印出一个网格

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


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

我卡住了,不知道从哪里开始使用这个这里是
raggedGrid
类的修改版本:

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
下面是一个示例用法:

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)

谢谢。我明白了,我只是想澄清一下“近距离”的含义?当我删除它时,我得到一个错误“”\uuuu init\uuuu()接受3个位置参数,但给出了5个“”。这意味着,在调用该函数的位置,参数列表中该位置允许出现零个或多个参数,而不仅仅是一个参数。在这里,我们希望在调用该函数时传递多个列长度,而不仅仅是一个列长度。
\uuuu init\uuuu()
的进一步简化如下:而不是
def\uu init(self,rows,*col\u length):
,只需使用
def\uu init(self,*col\u length):
。然后,在
\uuuu init\uuuu()
的主体内,您应该将
行的值作为
行=len(列长度)
获取。这意味着,在调用函数的地方,我们不必传递
行的值。我们只传递逗号分隔的列长度(现在已经传递了)。如果我们传递五个这样的列长度,这意味着
的值是
5