Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有更干净的方法来使用二维数组?_Python_Python 3.x_Multidimensional Array - Fatal编程技术网

Python 有没有更干净的方法来使用二维数组?

Python 有没有更干净的方法来使用二维数组?,python,python-3.x,multidimensional-array,Python,Python 3.x,Multidimensional Array,我试图创建一个2D数组类,但遇到了一个问题。我能想到的最好的方法是向get/setitem传递一个索引元组,并在函数中解包。但不幸的是,实现看起来非常混乱: class DDArray: data = [9,8,7,6,5,4,3,2,1,0] def __getitem__ (self, index): return (self.data [index [0]], self.data [index [1]]) def __setitem__ (sel

我试图创建一个2D数组类,但遇到了一个问题。我能想到的最好的方法是向get/setitem传递一个索引元组,并在函数中解包。但不幸的是,实现看起来非常混乱:

class DDArray:
    data = [9,8,7,6,5,4,3,2,1,0]

    def __getitem__ (self, index):
        return (self.data [index [0]], self.data [index [1]])

    def __setitem__ (self, index, value):
        self.data [index [0]] = value
        self.data [index [1]] = value

test = DDArray ()

print (test [(1,2)])

test [(1, 2)] = 120

print (test [1, 2])
我试着让它接受更多参数:

class DDArray:
    data = [9,8,7,6,5,4,3,2,1,0]

    def __getitem__ (self, index1, index2):
        return (self.data [index1], self.data [index2])

    def __setitem__ (self, index1, index2, value):
        self.data [index1] = value
        self.data [index2] = value

test = DDArray ()

print (test [1, 2])

test [1, 2] = 120

print (test [1, 2])
但这会导致一个奇怪的类型错误,告诉我我没有传递足够的参数(我猜下标运算符中的任何内容都被视为1个参数,即使有逗号)

(是的,我知道,上面的类实际上不是2D数组。我想在开始实际制作2D之前先弄清楚操作符。)

有没有一种看起来更干净的标准方法?
谢谢

有几种方法可以做到这一点。如果您想要像
test[1][2]
这样的语法,那么您可以让
\uuuuu getitem\uuuuuuuuuuu
返回一列(或行),可以使用
\uuuuuu getitem\uuuuuuuuuu
再次索引该列(或行)(甚至只返回一个列表)

但是,如果您想要语法
test[1,2]
,那么您的思路是正确的,
test[1,2]
实际上会将元组
(1,2)
传递给
\uu getitem\uuuuuuuuu
函数,因此在调用它时不需要包含偏执词

您可以使
\uuuu getitem\uuuuuuuuu
\uuuuuuu setitem\uuuuuuuuuuuuu
实现稍微不那么混乱,如下所示:

def __getitem__(self, indices):
    i, j = indices
    return (self.data[i], self.data[j])

当然,您实际实现了
\uuu getitem\uu
。关键是您已经将索引元组拆分为适当命名的变量。

有几种方法可以做到这一点。如果您想要像
test[1][2]
这样的语法,那么您可以让
\uuuuu getitem\uuuuuuuuuuu
返回一列(或行),可以使用
\uuuuuu getitem\uuuuuuuuuu
再次索引该列(或行)(甚至只返回一个列表)

但是,如果您想要语法
test[1,2]
,那么您的思路是正确的,
test[1,2]
实际上会将元组
(1,2)
传递给
\uu getitem\uuuuuuuuu
函数,因此在调用它时不需要包含偏执词

您可以使
\uuuu getitem\uuuuuuuuu
\uuuuuuu setitem\uuuuuuuuuuuuu
实现稍微不那么混乱,如下所示:

def __getitem__(self, indices):
    i, j = indices
    return (self.data[i], self.data[j])

当然,您实际实现了
\uuu getitem\uu
。关键是您已将索引元组拆分为适当命名的变量。

2d数组本机已受支持,为什么要编写自己的2d数组?@Hari Shankar我快速搜索了文档,并尝试使用=[]],但产生了语法错误。我会更仔细地查看文档。谢谢。在使用之前,需要将其初始化为列表列表(a=[[]])本机已支持2d数组,为什么要编写自己的2d数组?@Hari Shankar我快速搜索了文档,并尝试执行a=[]],但出现了语法错误。我会更仔细地查看文档。谢谢。在使用之前,需要将其初始化为列表列表(a=[[]])Hari实际上回答了我的问题,但不是作为答案,因此我将选择此选项,因为它也有帮助。最后一个带有“索引”和解包的语法就是这样做的方法。Hari实际上回答了我的问题,但不是作为答案,所以我会选择这个,因为它也有帮助。最后一个语法是“索引”和解包。