简体   繁体   English

在python中从2D数组类创建对象

[英]Create an object from 2D array class in python

Currently I'm having a data structure course using python. 目前,我正在使用python进行数据结构课程。 In the book, they have included two different classes to implement the 1D array structure and the 2D array. 在书中,他们包括了两个不同的类来实现1D数组结构和2D数组。

For 1D array: 对于一维数组:

import ctypes

class Array:

    def __init__(self, size):
        assert size > 0, "Array size must be > 0"
        self._size = size
        PyArrayType = ctypes.py_object * size
        self._elements = PyArrayType()
        self.clear(None)

    def len(self):
        return self._size

    def getitem(self, index):
        assert index >= 0 and index < self.len(), "Array subscript out of range"
        return self._elements[index]

    def setitem(self, index, value):
        assert 0 <= index < self.len(), "Array subscript out of range"
        self._elements[index] = value

    def clear(self, value):
        for i in range(self.len()):
            self._elements[i] = value

    def iter(self):
        return ArrayIterator(self._elements )

class ArrayIterator:
    def __init__(self, theArray):
        self._arrayRef = theArray
        self._curNdx = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self._curNdx < len(self._arrayRef):
            entry = self._arrayRef[self._curNdx]
            self._curNdx += 1
            return entry
        else:
            raise StopIteration

class Array2D :

    def __init__( self, numRows, numCols ):
        self._theRows = Array( numRows )
        for i in range( numRows ) :
            self._theRows[i] = Array( numCols )

    def numRows( self ):
        return len( self._theRows )

    def numCols( self ):
        return len( self._theRows[0] )

    def clear( self, value ):
        for row in range( self.numRows() ):
            row.clear( value )

    def __getitem__( self, ndxTuple ):
        assert len(ndxTuple) == 2, "Invalid number of array subscripts."
        row = ndxTuple[0]
        col = ndxTuple[1]
        assert row >= 0 and row < self.numRows() \
            and col >= 0 and col < self.numCols(), \
               "Array subscript out of range."
        the1dArray = self._theRows[row]

        return the1dArray[col]

    def __setitem__( self, ndxTuple, value ):
        assert len(ndxTuple) == 2, "Invalid number of array subscripts."
        row = ndxTuple[0]
        col = ndxTuple[1]
        assert row >= 0 and row < self.numRows() \
            and col >= 0 and col < self.numCols(), \
                "Array subscript out of range."
       the1dArray = self._theRows[row]
       the1dArray[col] = value

I use the following code to understand how it works: 我使用以下代码来了解其工作原理:

arr = Array(5)
arrLen = arr.len()
arr.clear(0)
for i in range (arrLen):
   print arr.getitem(i)

print "The length of the array = ",arrLen

print "Enter 5 numbers"
for i in range (arrLen):
#n = raw_input("num = ")
arr.setitem(i,i)

for i in range (arrLen):
print arr.getitem(i)
print"values are ", arr.iter()

However, I didn't know how to call the 2D array to understand how it works. 但是,我不知道如何调用2D数组以了解其工作原理。

ArrMulti = Array2D(3, 4)

and I got the following error: 我收到以下错误:

File "MultiArrayADT.py", line 46, in __init__
   self._theRows[i] = Array( numCols )
AttributeError: Array instance has no attribute '__setitem__'

So please can someone tell me how to create a 2D array object. 因此,请有人告诉我如何创建2D数组对象。

In your Array class you should override method __setitem__() , while you have implemented setitem() . 在实现setitem()同时,应在Array类中重写__setitem__()方法。 So try to change a name of the method. 因此,请尝试更改方法的名称。 That should work. 那应该工作。 Same with getitem() . getitem()相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM