簡體   English   中英

在python中從2D數組類創建對象

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

目前,我正在使用python進行數據結構課程。 在書中,他們包括了兩個不同的類來實現1D數組結構和2D數組。

對於一維數組:

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

我使用以下代碼來了解其工作原理:

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()

但是,我不知道如何調用2D數組以了解其工作原理。

ArrMulti = Array2D(3, 4)

我收到以下錯誤:

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

因此,請有人告訴我如何創建2D數組對象。

在實現setitem()同時,應在Array類中重寫__setitem__()方法。 因此,請嘗試更改方法的名稱。 那應該工作。 getitem()相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM