圆月山庄资源网 Design By www.vgjia.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
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程序设计有所帮助。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月18日
2024年11月18日
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】