欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2014年10月09日 11:03:22   投稿:shichen2014  
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之Array用法實例,較為詳細(xì)的講述了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程序設(shè)計有所幫助。

相關(guān)文章

  • Python實現(xiàn)線性判別分析(LDA)的MATLAB方式

    Python實現(xiàn)線性判別分析(LDA)的MATLAB方式

    今天小編大家分享一篇Python實現(xiàn)線性判別分析(LDA)的MATLAB方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 解決jupyter notebook顯示不全出現(xiàn)框框或者亂碼問題

    解決jupyter notebook顯示不全出現(xiàn)框框或者亂碼問題

    這篇文章主要介紹了解決jupyter notebook顯示不全出現(xiàn)框框或者亂碼問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python和flask中返回JSON數(shù)據(jù)的方法

    python和flask中返回JSON數(shù)據(jù)的方法

    下面小編就為大家整理了一篇python和flask中返回JSON數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Python內(nèi)置模塊turtle繪圖詳解

    Python內(nèi)置模塊turtle繪圖詳解

    這篇文章主要介紹了Python內(nèi)置模塊turtle繪圖詳解,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • python實現(xiàn)web應(yīng)用框架之增加動態(tài)路由

    python實現(xiàn)web應(yīng)用框架之增加動態(tài)路由

    這篇文章主要介紹web應(yīng)用框架如何添加動態(tài)路由,在我們編寫的框架中,我們添加動態(tài)路由,是使用了正則表達(dá)式,同時在注冊的時候,需要注明該路由是請求路由,文中有詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-05-05
  • Pytorch 保存模型生成圖片方式

    Pytorch 保存模型生成圖片方式

    今天小編就為大家分享一篇Pytorch 保存模型生成圖片方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • pytorch?K折交叉驗證過程說明及實現(xiàn)方式

    pytorch?K折交叉驗證過程說明及實現(xiàn)方式

    這篇文章主要介紹了pytorch?K折交叉驗證過程說明及實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Django各種緩存的配置小結(jié) ?

    Django各種緩存的配置小結(jié) ?

    本文主要介紹了Django各種緩存的配置小結(jié) ,Django提供了多種緩存后端,如內(nèi)存緩存、文件緩存、數(shù)據(jù)庫緩存、Memcached和Redis等,根據(jù)項目需求選擇合適的緩存后端,下面就一起來了解一下吧
    2023-08-08
  • python單線程文件傳輸?shù)膶嵗?C/S)

    python單線程文件傳輸?shù)膶嵗?C/S)

    今天小編就為大家分享一篇python單線程文件傳輸?shù)膶嵗?C/S),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • Python實現(xiàn)微信表情包炸群功能

    Python實現(xiàn)微信表情包炸群功能

    這篇文章主要介紹了Python實現(xiàn)微信表情包炸群功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01

最新評論