擅长:python、mysql、java
<p>对于您的问题,最简单的解决方案可能是使用可调用的tu来构建填充值,以便在矩阵中有不同的实例。在</p>
<pre><code> class GrowingList(list):
def __init__(self, fill):
super().__init__()
self.fill = fill
def __setitem__(self, index, value):
if index > len(self) - 1:
self.extend(self.fill() for i in range(index + 1 - len(self)))
list.__setitem__(self, index, value)
</code></pre>
<p>请注意,fill现在是可调用的,因此需要将其包装在函数中:</p>
^{pr2}$
<p>你也不能依赖项乘法这个习惯用法,因为它只是重复值<em>n</em>次。为了避免这种情况,需要使用列表理解。在</p>
<p>但是,这种方法仍然存在一个问题:</p>
^{3}$
<p>您可能需要考虑使用Blckknght建议的分散矩阵字典。在</p>