最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Python数据结构之Array用法实例

来源:动视网 责编:小采 时间:2020-11-27 14:40:47
文档

Python数据结构之Array用法实例

Python数据结构之Array用法实例:本文实例讲述了python数据结构之Array用法,分享给大家供大家参考。具体方法如下: import ctypes class Array: def __init__(self, size): assert size > 0, Array size must be > 0 self._size = size py
推荐度:
导读Python数据结构之Array用法实例:本文实例讲述了python数据结构之Array用法,分享给大家供大家参考。具体方法如下: import ctypes class Array: def __init__(self, size): assert size > 0, Array size must be > 0 self._size = size py


本文实例讲述了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 =0 and col= 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程序设计有所帮助。

文档

Python数据结构之Array用法实例

Python数据结构之Array用法实例:本文实例讲述了python数据结构之Array用法,分享给大家供大家参考。具体方法如下: import ctypes class Array: def __init__(self, size): assert size > 0, Array size must be > 0 self._size = size py
推荐度:
标签: 使用 ar 实例
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top