This article illustrates the array usage of Python data structures and shares them for your reference. The specific method is as follows:
Import cTYPES class Array:def __init__ (self, size): Assert size > 0, "Array size must be > 0" sel f._size = Size Pyarraytype = ctypes.py_object * Size self._elements = Pyarraytype () self.clear (None) d
EF Clear (Self, value): For index in range (Len (self)): Self._elements[index] = value def __len__ (self): Return self._size def __getitem__ (self, Index): Assert index >= 0 and Index < Len (self), "index must >=0 and <= size "return Self._elements[index] def __setitem__ (self, Index, value): Assert index >= 0 and Index < Len (self), "index must >=0 and <= size" self._elements[index] = value def __iter__ (self) : Return _arrayiterator (Self._elements) class _arrayiterator:def __init__ (Self, thearray): Self._arrayref = TheArray Self._curndr = 0 def __next__ (self): if Self._curndr < Len (thearray): entry = Self._a
RRAYREF[SELF._CURNDR] Sllf._curndr + 1 Return entry else:raise stopiteration def __iter__ (self): return self
Class Array2d:def __init__ (self, NumRows, numcols): Self._therows = Array (numcols) for I in range (Numcols)
: 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): Self._therows[r Ow].clear (value) def __getitem__ (self, ndxtuple): Assert len (ndxtuple) = 2, "The tuple must 2" row = Ndxtu Ple[0] col = ndxtuple[1] assert row>=0 and Row <len (Self.numrows ()) \ and col>=0 and Col<len (SE Lf.numcols), \ "Array subscrpt out of range" TheArray = Self._therows[row] return Thearray[col] def __ Setitem__ (self, Ndxtuple, value): Assert Len (ndxtuple) ==2, "The tuple must 2" row = ndxtuple[0] Col = ndxtu
Ple[1] Assert row >= 0 and Row < Len (self.numrows) and Col >= 0 and Col < Len (self.numcols), \ "Row and Col is InvaliDate "TheArray = Self._therows[row];
Thearray[col] = value
I hope this article will help you with your Python programming.