楼主: CDA网校
1090 1

[程序分享] Python数据结构之Array用法实例——CDA人工智能学院 [推广有奖]

管理员

已卖:189份资源

泰斗

3%

还不是VIP/贵宾

-

威望
3
论坛币
117887 个
通用积分
10225.2437
学术水平
278 点
热心指数
286 点
信用等级
253 点
经验
228030 点
帖子
6909
精华
19
在线时间
4373 小时
注册时间
2019-9-13
最后登录
2025-12-31

初级热心勋章

楼主
CDA网校 学生认证  发表于 2021-1-11 09:19:46 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
CDA人工智能学院致力于以优质的人工智能在线教育资源助力学员的DT职业梦想!课程内容涵盖数据分析机器学习深度学习人工智能tensorFlowPyTorch知识图谱等众多核心技术及行业案例,让每一个学员都可以在线灵活学习,快速掌握AI时代的前沿技术。PS:私信我即可获取CDA会员1个月免费试听机会
这篇文章主要介绍了Python数据结构之Array用法实例,较为详细的讲述了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 = 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程序设计有所帮助。

29C446FFF799701B5098749DB47B5891.jpg


扫码关注CDA公众号,即可获取最新版数据分析题库大全CDA免费精品课70+


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python array 数据结构 人工智能 CDA

沙发
三重虫 发表于 2021-1-11 13:46:28

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-2 21:23