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 xml解析实例详解
Nov 14 Python
使用pyecharts无法import Bar的解决方案
Apr 23 Python
在Python中执行系统命令的方法示例详解
Sep 14 Python
Python将一个Excel拆分为多个Excel
Nov 07 Python
python调用c++ ctype list传数组或者返回数组的方法
Feb 13 Python
python django生成迁移文件的实例
Aug 31 Python
Python3 tkinter 实现文件读取及保存功能
Sep 12 Python
解决python 读取excel时 日期变成数字并加.0的问题
Oct 08 Python
Python urlopen()和urlretrieve()用法解析
Jan 07 Python
为什么是 Python -m
Jun 19 Python
协程Python 中实现多任务耗资源最小的方式
Oct 19 Python
用Python爬取各大高校并可视化帮弟弟选大学,弟弟直呼牛X
Jun 11 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面向对象编程之深入理解方法重载与方法覆盖(多态)
2015/12/24 PHP
权威JavaScript 中的内存泄露模式
2007/08/13 Javascript
获取当前网页document.url location.href区别总结
2008/05/10 Javascript
Iframe thickbox2.0使用的方法
2009/03/05 Javascript
网络之美 JavaScript中Get和Set访问器的实现代码
2010/09/19 Javascript
JavaScript 原型继承
2011/12/26 Javascript
JS左右无缝滚动(一般方法+面向对象方法)
2012/08/17 Javascript
javascript 判断中文字符长度的函数代码
2012/08/27 Javascript
浅谈JSON.parse()和JSON.stringify()
2015/07/14 Javascript
jquery点击改变class并toggle的实现代码
2016/05/15 Javascript
微信小程序 购物车简单实例
2016/10/24 Javascript
基于jQuery实现的幻灯图片切换
2016/12/02 Javascript
Vue2.0利用 v-model 实现组件props双向绑定的优美解决方案
2017/03/13 Javascript
Angular8 实现table表格表头固定效果
2020/01/03 Javascript
解决vue页面刷新,数据丢失的问题
2020/11/24 Vue.js
微信小程序input抖动问题的修复方法
2021/03/03 Javascript
Python利用pyHook实现监听用户鼠标与键盘事件
2014/08/21 Python
使用Nginx+uWsgi实现Python的Django框架站点动静分离
2016/03/21 Python
python网络编程调用recv函数完整接收数据的三种方法
2017/03/31 Python
Python科学画图代码分享
2017/11/29 Python
Python函数any()和all()的用法及区别介绍
2018/09/14 Python
Python逐行读取文件中内容的简单方法
2019/02/26 Python
django组合搜索实现过程详解(附代码)
2019/08/06 Python
python wxpython 实现界面跳转功能
2019/12/17 Python
python中提高pip install速度
2020/02/14 Python
torchxrayvision包安装过程(附pytorch1.6cpu版安装)
2020/08/26 Python
使用css如何制作时间ICON方法实践
2012/11/12 HTML / CSS
阿迪达斯印度官方商城:adidas India
2017/03/26 全球购物
英国绿色商店:Natural Collection
2019/05/03 全球购物
便利店投资创业计划书
2014/02/08 职场文书
读群众路线的心得体会
2014/09/03 职场文书
护士长2014年度工作总结
2014/11/11 职场文书
采购员岗位职责
2015/02/03 职场文书
SQL Server数据定义——模式与基本表操作
2021/04/05 SQL Server
Python爬虫之爬取哔哩哔哩热门视频排行榜
2021/04/28 Python
JavaScript执行机制详细介绍
2021/12/06 Javascript