class MyArray(object):

def __init__(self, values):

#values can be of any kinds which can be converted into list

self.__data = list(values)

#多下标访问列表元素

def __getitem__(self, index):

length = len(self.__data)

#index can be an integer

if isinstance(index, int) and index<length:

return self.__data[index]

#index can also be a list/tuple which has many integers

elif isinstance(index, (list,tuple)):

#ensure that all integers given must < the length of data

for i in index:

if not (isinstance(i,int) and i<length):

return 'index error'

result = []

for item in index:

result.append(self.__data[item])

return result

else:

return 'index error'

#多下标元素赋值

def __setitem__(self, index, value):

length = len(self.__data)

if isinstance(index, int) and index<length:

self.__data[index] = value

elif isinstance(index, (list,tuple)):

for i in index:

if not (isinstance(i,int) and i<length):

raise Exception('index error')

if isinstance(value, (list,tuple)):

if len(index) == len(value):

for i, v in enumerate(index):

self.__data[v] = value[i]

else:

raise Exception('values and index must be of the same length')

elif isinstance(value, (int,float,complex,str)):

for i in index:

self.__data[i] = value

else:

raise Exception('value error')

else:

raise Exception('index error')

def __repr__(self):

return str(self.__data)

用法演示:

>>> from myArr import MyArray

>>> x = MyArray(range(20))

>>> x

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

>>> x[3]

3

>>> x[3] = 'a'

>>> x

[0, 1, 2, 'a', 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

>>> x[[3,3,5]]

['a', 'a', 5]

>>> x[[3,3,5]] = ['test', 'hello', 100]

>>> x

[0, 1, 2, 'hello', 4, 100, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

>>> x[[1,3,5]] = ['test', 'hello', 100]

>>> x

[0, 'test', 2, 'hello', 4, 100, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

>>> x[100]

'index error'

>>> x[100] = 3

Traceback (most recent call last):

File "<pyshell#90>", line 1, in <module>

x[100] = 3

File "C:/Python35\myArr.py", line 44, in __setitem__

raise Exception('index error')

Exception: index error

>>> x[[1,100]] = 3

Traceback (most recent call last):

File "<pyshell#91>", line 1, in <module>

x[[1,100]] = 3

File "C:/Python35\myArr.py", line 31, in __setitem__

raise Exception('index error')

Exception: index error

>>> x[list(range(20,2))] = 0

>>> x

[0, 'test', 2, 'hello', 4, 100, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

>>> y=list(range(20,2))

>>> x[y] = 0

>>> x

[0, 'test', 2, 'hello', 4, 100, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

>>> x[[1,3,5,7,9]] = 0

>>> x

[0, 0, 2, 0, 4, 0, 6, 0, 8, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

封装Python列表实现多下标访问相关推荐

  1. 微课系列(三):Python列表中存储的是元素的引用

    技术要点:在Python中,变量不直接存储值,而是存储值的引用.同样,在列表.元组.字典.集合等容器类对象中也是存储的元素值的引用. 以列表为例,当列表与整数相乘进行元素重复时,是对引用进行重复.这样 ...

  2. Fibonacci数列第n项的第7种计算方法:Python列表

    前面已经分享了几种计算Fibonacci数列第n项的方法,详见Python快速计算Fibonacci数列中第n项的方法和三种Fibonacci数列第n项计算方法及其优劣分析,本文分享第7种(过几天分享 ...

  3. python 多维list 排序_人生苦短 | Python列表和元组归纳整理

    1. 列表 1.1. 列表是什么? 列表是由一系列按特定顺序排列的元素组成,所以是有序的集合.列表中的元素可以是不同类型的,列表中也可以嵌套列表.在Python中,用方括号[]来表示列表,并用逗号来分 ...

  4. 【Python学习教程】Python列表(list)、元组(tuple)、字典(dict)和集合(set)详解

    文章目录 什么是序列,Python序列详解(包括序列类型和常用操作) 序列索引 序列切片 序列相加 序列相乘 检查元素是否包含在序列中 和序列相关的内置函数 Python list列表详解 Pytho ...

  5. Python教程:python中二维列表的创建、访问、应用详解

    欢迎你来到站长在线的站长学堂学习Python知识,本文学习的是<Python中二维列表的创建.访问.应用详解>.本知识点主要内容有:二维列表的概念.直接定义二维列表.使用嵌套的for循环创 ...

  6. python列表元素下标是什么_python列表中元素插入位置总结

    python列表中元素插入位置总结 , python中列表去掉最后一个元素 ist.insert(index,obj) 列表与方法之间用点号相隔,括号内需要添入的参数分别是索引和要插入的元素. 要完成 ...

  7. Python 列表下标操作

    Python  列表下标操作 引用网址: https://www.jianshu.com/p/a98e935e4d46 转载于:https://www.cnblogs.com/zenghanxi/p/ ...

  8. python sorted下标_初学者掌握python 列表需要知道的操作

    为了感谢大家对"Python客栈"的关注与支持,我们每天会在留言中随机抽取三位粉丝发放6.6元小红包.快来参与吧!前些日子我们介绍了 python中基本的数据结构--元组,今天我们 ...

  9. python不支持下标访问元素吗_Python 集合不支持使用下标访问其中的元素

    Python 集合不支持使用下标访问其中的元素 答:对 下列对于新创企业的优势的描述中,错误的是(). 答:开拓新市场投资多,但潜在的回报少 与欧洲近代科学革命相比,18世纪美国科学发展的优势是() ...

最新文章

  1. Windows 7 IIS (HTTP Error 500.21 - Internal Server Error)解决
  2. 413 Request Entity Too Large 的解决方法
  3. SmartArt使用
  4. arthas命令使用示例:watch
  5. 【新无人机数据集】从行人重识别 到 无人机目标定位
  6. 携程微服务框架实践及思考
  7. Vertica DBD 分析优化设计
  8. 微课|玩转Python轻松过二级(1.1节):Python命令式编程与函数式编程模式
  9. X86汇编语言从实模式到保护模式07:硬盘和显卡的访问控制
  10. 【codecombat】 试玩全攻略 第十六关 近战
  11. 怎样在虚拟机装文件服务器,虚拟机下怎么解压文件
  12. Hibernate VS iBATIS (转自ITEYE davy)
  13. ACdream - 1069 - 无耻的出题人
  14. 酒香还怕巷子深?如何打造一个优秀的GitHub开源项目
  15. 字节跳动-后端开发岗最新春招面经分享,四面拿下,有惊无险
  16. Grafana任意文件读取
  17. 爬取6874条数据,告诉你数据分析师的薪资待遇~!
  18. 图解windows xp Professional系统安装过程
  19. iOS小技能:设置tableView的点击事件优先级低于cell的选中事件(场景:比如筛选视图,监听蒙版的点击事件就隐藏筛选视图)
  20. 计算机游戏的作文,玩电脑游戏作文400字

热门文章

  1. matlab卷积画图,基于MATLAB的卷积演示系统课程设计
  2. phpcms9.6 ueditor_Phpcms v9深度整合百度编辑器Ueditor
  3. 服务器中修改项目端口,c#-在Visual Studio 2013中更改项目端口号
  4. java tcp 线程_java 网络协议(一)Tcp多线程服务器端编程
  5. java商城项目中多线程执行_java多线程中执行多个程序的实例分析
  6. java基础大概_Java基础知识(一)
  7. 基于JAVA+SpringMVC+Mybatis+MYSQL的二手电动车交易系统
  8. 基于JAVA的IKAnalyzer中文分词运用
  9. 第三周笔记 c++ Boolan
  10. A. 面向对象思想介绍