如何在python中定义动态二维数组?

发布于 2024-11-19 21:27:22 字数 231 浏览 4 评论 0原文

我想在python中定义一个动态二维数组。

我不知道我的二维数组在程序开始时有多少行。我想根据需要在此二维数组中定义新行。

就像这段代码一样:

array = [][100]
array[0][]  = new array()
array[0][2] = 'hello'

array[1][] = new array()
array[1][3]= 'hello'

I want to define a dynamic two-dimensional array in python.

I don't know how many rows my two-dimensional array will have at the start of my program. I would like to define new rows in this 2D array as needed.

As like this code:

array = [][100]
array[0][]  = new array()
array[0][2] = 'hello'

array[1][] = new array()
array[1][3]= 'hello'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残龙傲雪 2024-11-26 21:27:22

您的意思是类似以下内容吗?

class DynamicList(list):

    def __getslice__(self, i, j):
        return self.__getitem__(slice(i, j))
    def __setslice__(self, i, j, seq):
        return self.__setitem__(slice(i, j), seq)
    def __delslice__(self, i, j):
        return self.__delitem__(slice(i, j))

    def _resize(self, index):
        n = len(self)
        if isinstance(index, slice):
            m = max(abs(index.start), abs(index.stop))
        else:
            m = index + 1
        if m > n:
            self.extend([self.__class__() for i in range(m - n)])

    def __getitem__(self, index):
        self._resize(index)
        return list.__getitem__(self, index)

    def __setitem__(self, index, item):
        self._resize(index)
        if isinstance(item, list):
            item = self.__class__(item)
        list.__setitem__(self, index, item)

>>> mat = DynamicList()
>>> mat[0] = ['row1','row1','row1']
>>> mat[1] = ['row2','row2']
>>> mat[2]= ['row3']
>>> mat
[['row1', 'row1', 'row1'], ['row2', 'row2'], ['row3']]
>>> print mat[1][1]
row2
>>> mat[5][5] = 'row5'
>>> mat
[['row1', 'row1', 'row1'], ['row2', 'row2'], ['row3'], [], [], [[], [], [], [], 
[], 'row5']]
>>> print mat[5]
[[], [], [], [], [], 'row5']
>>> print mat[5][5]
row5

Do you mean something like the following?

class DynamicList(list):

    def __getslice__(self, i, j):
        return self.__getitem__(slice(i, j))
    def __setslice__(self, i, j, seq):
        return self.__setitem__(slice(i, j), seq)
    def __delslice__(self, i, j):
        return self.__delitem__(slice(i, j))

    def _resize(self, index):
        n = len(self)
        if isinstance(index, slice):
            m = max(abs(index.start), abs(index.stop))
        else:
            m = index + 1
        if m > n:
            self.extend([self.__class__() for i in range(m - n)])

    def __getitem__(self, index):
        self._resize(index)
        return list.__getitem__(self, index)

    def __setitem__(self, index, item):
        self._resize(index)
        if isinstance(item, list):
            item = self.__class__(item)
        list.__setitem__(self, index, item)

>>> mat = DynamicList()
>>> mat[0] = ['row1','row1','row1']
>>> mat[1] = ['row2','row2']
>>> mat[2]= ['row3']
>>> mat
[['row1', 'row1', 'row1'], ['row2', 'row2'], ['row3']]
>>> print mat[1][1]
row2
>>> mat[5][5] = 'row5'
>>> mat
[['row1', 'row1', 'row1'], ['row2', 'row2'], ['row3'], [], [], [[], [], [], [], 
[], 'row5']]
>>> print mat[5]
[[], [], [], [], [], 'row5']
>>> print mat[5][5]
row5
|煩躁 2024-11-26 21:27:22

使用 NumPy 的 矩阵类怎么样?

How about using NumPy's matrix class?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文