Python数据结构之Array用法实例


Posted in Python onOctober 09, 2014

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

Python 相关文章推荐
盘点提高 Python 代码效率的方法
Jul 03 Python
详解Python的Django框架中的中间件
Jul 24 Python
python机器学习实战之最近邻kNN分类器
Dec 20 Python
Python用sndhdr模块识别音频格式详解
Jan 11 Python
python使用锁访问共享变量实例解析
Feb 08 Python
python实现多进程代码示例
Oct 31 Python
想学python 这5本书籍你必看!
Dec 11 Python
基于PyQt4和PySide实现输入对话框效果
Feb 27 Python
python实现kmp算法的实例代码
Apr 03 Python
pandas dataframe的合并实现(append, merge, concat)
Jun 24 Python
python移位运算的实现
Jul 15 Python
pycharm中leetcode插件使用图文详解
Dec 07 Python
python中pygame模块用法实例
Oct 09 #Python
python根据文件大小打log日志
Oct 09 #Python
python命令行参数解析OptionParser类用法实例
Oct 09 #Python
python测试驱动开发实例
Oct 08 #Python
python批量提交沙箱问题实例
Oct 08 #Python
python求pi的方法
Oct 08 #Python
python实现简单的TCP代理服务器
Oct 08 #Python
You might like
破解.net程序(dll文件)编译和反编译方法
2013/01/31 PHP
PHP使用range协议实现输出文件断点续传代码实例
2014/07/04 PHP
分享自定义的几个PHP功能函数
2015/04/15 PHP
PHP实现无限极分类的两种方式示例【递归和引用方式】
2019/03/25 PHP
JavaScript中原型和原型链详解
2015/02/11 Javascript
js实现图片漂浮效果的方法
2015/03/02 Javascript
JS实现点击文字对应DIV层不停闪动效果的方法
2015/03/02 Javascript
jQuery实现为图片添加镜头放大效果的方法
2015/06/25 Javascript
javascript跨域总结之window.name实现的跨域数据传输
2015/11/01 Javascript
js为什么不能正确处理小数运算?
2015/12/29 Javascript
jquery中关于bind()方法的使用技巧分享
2017/03/30 jQuery
微信小程序 setData使用方法及常用错误解决办法
2017/05/11 Javascript
详解angularjs 关于ui-router分层使用
2017/06/12 Javascript
使用jQuery实现购物车结算功能
2017/08/15 jQuery
基于JavaScript实现前端数据多条件筛选功能
2020/08/19 Javascript
微信小程序swiper组件用法实例分析【附源码下载】
2017/12/07 Javascript
浅谈Vue-cli单文件组件引入less,sass,css样式的不同方法
2018/03/13 Javascript
nuxt配置通过指定IP和端口访问的实现
2020/01/08 Javascript
[40:01]OG vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
Python中装饰器的一个妙用
2015/02/08 Python
详解Python中break语句的用法
2015/05/14 Python
Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
2018/09/14 Python
Opencv-Python图像透视变换cv2.warpPerspective的示例
2019/04/11 Python
Python3中的最大整数和最大浮点数实例
2019/07/09 Python
解决python 读取 log日志的编码问题
2019/12/24 Python
解决导入django_filters不成功问题No module named 'django_filter'
2020/07/15 Python
Python自动创建Excel并获取内容
2020/09/16 Python
英国领先的汽车轮胎和快速健康中心:Kwik Fit
2017/10/29 全球购物
民主评议党员自我评价材料
2014/09/18 职场文书
安全保证书
2015/01/16 职场文书
校运会通讯稿
2015/07/18 职场文书
2015国庆节放假通知范文
2015/07/30 职场文书
高中班主任培训心得体会
2016/01/07 职场文书
《敬重卑微》读后感3篇
2019/11/26 职场文书
十大最强火系宝可梦,喷火龙上榜,第一名有双火属性
2022/03/18 日漫
Javascript中Microtask和Macrotask鲜为人知的知识点
2022/04/02 Javascript