最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python數(shù)據(jù)結(jié)構(gòu)之Array用法實例

 更新時間:2014年10月09日 11:03:22   投稿:shichen2014  
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之Array用法實例,較為詳細的講述了Array的常見用法,具有很好的參考借鑒價值,需要的朋友可以參考下

本文實例講述了python數(shù)據(jù)結(jié)構(gòu)之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程序設計有所幫助。

相關(guān)文章

  • 淺談pandas中DataFrame關(guān)于顯示值省略的解決方法

    淺談pandas中DataFrame關(guān)于顯示值省略的解決方法

    下面小編就為大家分享一篇淺談pandas中DataFrame關(guān)于顯示值省略的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 詳解Pandas與openpyxl庫的超強結(jié)合

    詳解Pandas與openpyxl庫的超強結(jié)合

    Pandas絕對是Python中處理Excel最快、最好用的庫,但是使用 openpyxl 的一些優(yōu)勢是能夠輕松地使用樣式、條件格式等自定義電子表格,感興趣的可以了解一下
    2021-09-09
  • Python+Redis從零打造分布式鎖實戰(zhàn)示例

    Python+Redis從零打造分布式鎖實戰(zhàn)示例

    Redis作為一款高性能的內(nèi)存鍵值數(shù)據(jù)庫,憑借其支持原子操作、高并發(fā)和數(shù)據(jù)持久化等特性,非常適合用來實現(xiàn)分布式鎖,本文將詳細探討如何使用Python結(jié)合Redis從簡單到復雜地實現(xiàn)分布式鎖,并提供相應的示例代碼
    2024-01-01
  • Python文本處理簡單易懂方法解析

    Python文本處理簡單易懂方法解析

    這篇文章主要介紹了Python文本處理簡單易懂方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • 基于Python實現(xiàn)圍棋游戲的示例代碼

    基于Python實現(xiàn)圍棋游戲的示例代碼

    今天給大家?guī)硪黄趪宓脑创a分享。下面我們先看看效果。游戲進去默認為九路玩法,當然也可以選擇十三路或是十九路玩法,感興趣的可以了解一下
    2022-05-05
  • Pandas index操作索引的實現(xiàn)

    Pandas index操作索引的實現(xiàn)

    Pandas中的索引index用于選擇特定的行數(shù)和列數(shù),加快數(shù)據(jù)訪問速度,本文就來介紹一下index操作索引,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧
    2025-01-01
  • 淺談pyqt5中信號與槽的認識

    淺談pyqt5中信號與槽的認識

    這篇文章主要介紹了淺談pyqt5中信號與槽的認識,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    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()的用法(詳解)

    下面小編就為大家?guī)硪黄狿ython中index()和seek()的用法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • 淺析PyCharm 的初始設置(知道)

    淺析PyCharm 的初始設置(知道)

    這篇文章主要介紹了PyCharm 的初始設置(知道),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10

最新評論

曲靖市| 定陶县| 开化县| 台东县| 济阳县| 衡阳市| 云阳县| 通海县| 乐至县| 新安县| 祁门县| 南皮县| 博乐市| 威远县| 崇阳县| 阿拉善左旗| 通州市| 阿拉尔市| 康定县| 吴忠市| 南陵县| 资中县| 花莲市| 奉贤区| 新密市| 柏乡县| 阿鲁科尔沁旗| 桓台县| 吉木萨尔县| 余干县| 石渠县| 同德县| 清苑县| 永州市| 茶陵县| 凭祥市| 花垣县| 鲁山县| 博野县| 浮山县| 电白县|