Example of array usage for Python data structures _python

Source: Internet
Author: User
Tags assert data structures

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.