国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python數據結構之Array用法實例

2020-02-23 06:04:33
字體:
來源:轉載
供稿:網友

本文實例講述了python數據結構之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 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 

希望本文所述對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沈丘县| 新源县| 芦溪县| 嘉定区| 武城县| 霞浦县| 大姚县| 东兰县| 乐山市| 武威市| 扎赉特旗| 韶关市| 龙门县| 凤庆县| 什邡市| 西安市| 乐陵市| 麻阳| 广平县| 年辖:市辖区| 西林县| 渝北区| 沧州市| 灵丘县| 延川县| 鄱阳县| 高碑店市| 蒙山县| 北流市| 岗巴县| 沂南县| 三河市| 塔河县| 海口市| 密云县| 鹤庆县| 道真| 密山市| 大理市| 雷山县| 桓仁|