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利用matplotlib库绘制饼图的方法示例
Dec 18 Python
Python 专题三 字符串的基础知识
Mar 19 Python
python读写json文件的简单实现
Apr 11 Python
Python解析命令行读取参数--argparse模块使用方法
Jan 23 Python
Flask解决跨域的问题示例代码
Feb 12 Python
python list数据等间隔抽取并新建list存储的例子
Nov 27 Python
Tensorflow训练MNIST手写数字识别模型
Feb 13 Python
PyCharm 专业版安装图文教程
Feb 20 Python
Python+OpenCV实现图像的全景拼接
Mar 05 Python
python图片指定区域替换img.paste函数的使用
Apr 09 Python
Pytorch环境搭建与基本语法
Jun 03 Python
pytorch训练神经网络爆内存的解决方案
May 22 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
无限级别菜单的实现
2006/10/09 PHP
简单的PHP缓存设计实现代码
2011/09/30 PHP
phpcms的分类名称和类别名称的调用
2017/01/05 PHP
Laravel框架使用Redis的方法详解
2018/05/30 PHP
ThinkPHP中获取指定日期后工作日的具体日期方法
2018/10/14 PHP
PHP安装扩展mcrypt以及相关依赖项深入讲解
2021/03/04 PHP
jQuery autocomplete插件修改
2009/04/17 Javascript
Javascript学习笔记8 用JSON做原型
2010/01/11 Javascript
如何判断鼠标是否在DIV的区域内
2013/11/13 Javascript
使用jquery清空、复位整个输入域
2015/04/02 Javascript
JavaScript实现阿拉伯数字和中文数字互相转换
2016/06/12 Javascript
项目实践一图片上传之form表单还是base64前端图片压缩(前端图片压缩)
2016/07/28 Javascript
简单谈谈JS数组中的indexOf方法
2016/10/13 Javascript
webuploader模态框ueditor显示问题解决方法
2016/12/27 Javascript
vue-resourse将json数据输出实例
2017/03/08 Javascript
使用bootstrap-paginator.js 分页来进行ajax 异步分页请求示例
2017/03/09 Javascript
node.js中EJS 模板快速入门教程
2017/05/08 Javascript
AngularJS 支付倒计时功能实现思路
2017/06/05 Javascript
浅谈vuex 闲置状态重置方案
2018/01/04 Javascript
利用Console来Debug的10个高级技巧汇总
2018/03/26 Javascript
Vuejs+vue-router打包+Nginx配置的实例
2018/09/20 Javascript
JS插件amCharts实现绘制柱形图默认显示数值功能示例
2019/11/26 Javascript
webpack打包优化的几个方法总结
2020/02/10 Javascript
解决vue项目打包上服务器显示404错误,本地没出错的问题
2020/11/03 Javascript
[01:00:53]2018DOTA2亚洲邀请赛3月29日 小组赛B组 iG VS Secret
2018/03/30 DOTA
Django框架实现逆向解析url的方法
2018/07/04 Python
Django中的Model操作表的实现
2018/07/24 Python
解读python如何实现决策树算法
2018/10/11 Python
Python实现对adb命令封装
2020/03/06 Python
法国时尚童装网站:Melijoe
2016/08/10 全球购物
法人委托书范本
2014/04/04 职场文书
成绩单公证书
2014/04/10 职场文书
家长会欢迎词
2015/01/23 职场文书
工程项目合作意向书
2015/05/08 职场文书
自荐信范文
2019/05/20 职场文书
七个非常实用的Python工具包总结
2021/06/15 Python