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编写网页爬虫脚本并实现APScheduler调度
Jul 28 Python
浅谈python日志的配置文件路径问题
Apr 28 Python
python获取命令行输入参数列表的实例代码
Jun 23 Python
彻底理解Python中的yield关键字
Apr 01 Python
Python使用正则表达式分割字符串的实现方法
Jul 16 Python
python通过http下载文件的方法详解
Jul 26 Python
Python range、enumerate和zip函数用法详解
Sep 11 Python
Python FFT合成波形的实例
Dec 04 Python
Python socket处理client连接过程解析
Mar 18 Python
python3 通过 pybind11 使用Eigen加速代码的步骤详解
Dec 07 Python
linux centos 7.x 安装 python3.x 替换 python2.x的过程解析
Dec 14 Python
python中yield的用法详解
Jan 13 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
php trim 去除空字符的定义与语法介绍
2010/05/31 PHP
php中使用explode查找某个字符是否存在的方法
2011/07/12 PHP
PHP获取当前url的具体方法全面解析
2013/11/26 PHP
php写入、删除与复制文件的方法
2015/06/20 PHP
PHP面相对象中的重载与重写
2017/02/13 PHP
php代码调试利器firephp安装与使用方法分析
2018/08/21 PHP
ThinkPHP5框架中使用JWT的方法示例
2020/06/03 PHP
Prototype Number对象 学习
2009/07/19 Javascript
JQuery对class属性的操作实现按钮开关效果
2013/10/11 Javascript
SeaJS入门教程系列之SeaJS介绍(一)
2014/03/03 Javascript
extjs 时间范围选择自动判断的实现代码
2014/06/24 Javascript
基于HTML+CSS,jQuery编写的简易计算器后续(添加了键盘监听)
2016/01/05 Javascript
js仿百度登录页实现拖动窗口效果
2016/03/11 Javascript
基于jQuery实现仿微博发布框字数提示
2016/07/27 Javascript
AngularJS变量及过滤器Filter用法分析
2016/11/22 Javascript
Vue 中使用 CSS Modules优雅方法
2018/04/09 Javascript
jQuery使用bind动态绑定事件无效的处理方法
2018/12/11 jQuery
微信小程序动画组件使用解析,类似vue,且更强大
2019/08/01 Javascript
JS使用H5实现图片预览功能
2019/09/30 Javascript
javascript实现点亮灯泡特效示例
2019/10/15 Javascript
vue使用recorder.js实现录音功能
2019/11/22 Javascript
JS快速实现简单计算器
2020/04/08 Javascript
NodeJS和浏览器中this关键字的不同之处
2021/03/03 NodeJs
[15:58]DOTA2国际邀请赛采访专栏:Tongfu.Sansheng&KingJ,DK.rOtk
2013/08/08 DOTA
python实现rsa加密实例详解
2017/07/19 Python
Python实现进程同步和通信的方法
2018/01/02 Python
python3 发送任意文件邮件的实例
2018/01/23 Python
使用Python制作新型冠状病毒实时疫情图
2020/01/28 Python
幼师专业求职推荐信
2013/11/08 职场文书
学习雷锋精神心得体会范文
2014/03/12 职场文书
个人培训自我鉴定
2014/03/28 职场文书
交通事故责任认定书
2015/08/06 职场文书
学习党史心得体会2016
2016/01/23 职场文书
个人向公司借款协议书
2016/03/19 职场文书
Go语言使用select{}阻塞main函数介绍
2021/04/25 Golang
Python实现列表拼接和去重的三种方式
2021/07/02 Python