Python数据结构之Array用法实例

 更新时间:2014年10月09日 11:03:22   投稿:shichen2014  
这篇文章主要介绍了Python数据结构之Array用法实例,较为详细的讲述了Array的常见用法,具有很好的参考借鉴价值,需要的朋友可以参考下

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

相关文章

  • 浅谈pandas中DataFrame关于显示值省略的解决方法

    浅谈pandas中DataFrame关于显示值省略的解决方法

    下面小编就为大家分享一篇浅谈pandas中DataFrame关于显示值省略的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • 详解Pandas与openpyxl库的超强结合

    详解Pandas与openpyxl库的超强结合

    Pandas绝对是Python中处理Excel最快、最好用的库,但是使用 openpyxl 的一些优势是能够轻松地使用样式、条件格式等自定义电子表格,感兴趣的可以了解一下
    2021-09-09
  • Python+Redis从零打造分布式锁实战示例

    Python+Redis从零打造分布式锁实战示例

    Redis作为一款高性能的内存键值数据库,凭借其支持原子操作、高并发和数据持久化等特性,非常适合用来实现分布式锁,本文将详细探讨如何使用Python结合Redis从简单到复杂地实现分布式锁,并提供相应的示例代码
    2024-01-01
  • Python文本处理简单易懂方法解析

    Python文本处理简单易懂方法解析

    这篇文章主要介绍了Python文本处理简单易懂方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • 基于Python实现围棋游戏的示例代码

    基于Python实现围棋游戏的示例代码

    今天给大家带来一期围棋的源码分享。下面我们先看看效果。游戏进去默认为九路玩法,当然也可以选择十三路或是十九路玩法,感兴趣的可以了解一下
    2022-05-05
  • Pandas index操作索引的实现

    Pandas index操作索引的实现

    Pandas中的索引index用于选择特定的行数和列数,加快数据访问速度,本文就来介绍一下index操作索引,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2025-01-01
  • 浅谈pyqt5中信号与槽的认识

    浅谈pyqt5中信号与槽的认识

    这篇文章主要介绍了浅谈pyqt5中信号与槽的认识,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • Python3.7 新特性之dataclass装饰器

    Python3.7 新特性之dataclass装饰器

    Python 3.7中一个令人兴奋的新特性是 data classes 。这篇文章主要介绍了Python3.7 新特性之dataclass装饰器,需要的朋友可以参考下
    2019-05-05
  • Python中index()和seek()的用法(详解)

    Python中index()和seek()的用法(详解)

    下面小编就为大家带来一篇Python中index()和seek()的用法(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • 浅析PyCharm 的初始设置(知道)

    浅析PyCharm 的初始设置(知道)

    这篇文章主要介绍了PyCharm 的初始设置(知道),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10

最新评论