• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python数据结构之Array用法实例

Python数据结构之Array用法实例

作者:shichen2014 字体:[增加 减小] 来源:互联网

shichen2014 通过本文主要向大家介绍了python array,python np.array,python array函数,python中array,python numpy array等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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 

</div>
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 
</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • 详解Python中的array数组模块相关使用
  • 详解Python中的array数组模块相关使用
  • Python数据结构之Array用法实例
  • Python数据结构之Array用法实例

相关文章

  • python统计字符串中指定字符出现次数的方法
  • Python中基本的日期时间处理的学习教程
  • ubuntu系统下 python链接mysql数据库的方法
  • Python中使用第三方库xlutils来追加写入Excel文件示例
  • Python和JavaScript间代码转换的4个工具
  • 使用Python的web.py框架实现类似Django的ORM查询的教程
  • Django的数据模型访问多对多键值的方法
  • 深入解析Python中的变量和赋值运算符
  • Python求解平方根的方法
  • python实现的DES加密算法和3DES加密算法实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python中输出ASCII大文字、艺术字、字符字小技巧
    • python采用getopt解析命令行输入参数实例
    • 可用于监控 mysql Master Slave 状态的python代码
    • Python实现读取目录所有文件的文件名并保存到txt文件代码
    • Python使用ntplib库同步校准当地时间的方法
    • wxpython 学习笔记 第一天
    • 用Python进行基础的函数式编程的教程
    • Python中分数的相关使用教程
    • python用Pygal如何生成漂亮的SVG图像详解
    • Python动态加载模块的3种方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有