|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 ppp099 于 2022-4-14 17:45 编辑
已知list 列表类中没有insert_head方法,
写一个自定义的类MyList,继承自list类,在MyList类内添加
class MyList(list):
def insert_head(self, value):
'''以下自己实现,将Value插入到列表的开始处'''
如:
L = MyList(range(1,5))
print(L) # [1,2,3,4]
L.insert_head(0)
print(L) # [0,1,2,3,4]
L.append(5)
print(L) # [0,1,2,3,4,5]
list列表类应该怎么写
本帖最后由 isdkz 于 2022-4-14 17:34 编辑
- class MyList(list):
- def insert_head(self, value):
- '''将Value插入到列表的开始处'''
- self.insert(0, value) # 加了这一句
- L = MyList(range(1,5))
- print(L) # [1,2,3,4]
- L.insert_head(0)
- print(L) # [0,1,2,3,4]
- L.append(5)
- print(L) # [0,1,2,3,4,5]
复制代码
|
|