Python Data Structure-Array usage example, pythonarray

Source: Internet
Author: User

Python Data Structure-Array usage example, pythonarray

The example in this article describes the Array usage of the python data structure 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 "     self._size = size     pyArrayType = ctypes.py_object * size     self._elements = pyArrayType()     self.clear(None)    def 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._arrayRef[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[row].clear(value)    def __getitem__(self, ndxTuple):     assert len(ndxTuple) == 2, "the tuple must 2"     row = ndxTuple[0]     col = ndxTuple[1]     assert row>=0 and row <len(self.numRows()) \     and col>=0 and col<len(self.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 = ndxTuple[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 Python programming.


How to build a Python Data Structure

Python Data Structure 1. list: variable value assignment method: shoplist = ['apple', 'mango ', 'carrot', 'Banana'] 2. array: variable value assignment method: zoo = ('Wolf ', 'elephant', 'penguin') 3. dictionary dict: Variable assignment mode: d = {key1: value1, key2: value2} 4. sequence: List, tuples, and strings are all sequences. (1) index OPERATOR: Get a character (2) of an element or string in a list/tuples. (2) segment OPERATOR: get a slice of a sequence, continuous element/character (3) eg: name = 'swaroop 'print 'characters0is ', name [0] # 's' index operator, similar to C # print 'characters1to3is ', name [] 'wa' slicing operator, similar to the Substring method in C #. In fact, Python contains a lot of content, such as Python and traditional The interpreted script language is different. It will be compiled into bytecode during the first execution, and then runs the Code directly. net dlr is a bit similar to the source code sky, and similar to Java virtual machines. In short, the code is converted into a way closer to the machine code, which can improve performance.

How to Use Python Arrays

Equivalent code

A = [1, 2, 3]

For I in:

Print I

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.