首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >封装Python列表实现多下标访问

封装Python列表实现多下标访问

作者头像
Python小屋屋主
发布2018-04-16 14:15:51
9420
发布2018-04-16 14:15:51
举报
文章被收录于专栏:Python小屋Python小屋

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]

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-07-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python小屋 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档