python_collections_deque结构学习笔记

from collections import deque
# 创建一个空的deque
x=deque()
print(x)                            #deque([])
print(type(x))                      #<class 'collections.deque'>
#带初值
x=deque('12345')
print(x)                            #deque(['1', '2', '3', '4', '5'])
print(x[0])                         #1
x=deque(range(10),maxlen=5)         #deque([5, 6, 7, 8, 9], maxlen=5)
print(x)
x=deque([0,1,2,3,4,5,6,7,8],maxlen=6)
print(x)                            #deque([2, 3, 4, 5, 6], maxlen=5)

x=deque(range(1,5),maxlen=6)        #deque([1, 2, 3, 4], maxlen=6)\
#append(self, *args, **kwargs),和list的一样
x.append(5)
print(x)                            #deque([1, 2, 3, 4, 5], maxlen=6)
#appendleft(self, *args, **kwargs),类似于append(),但是左边
x.appendleft(0)
print(x)                            #deque([0, 1, 2, 3, 4, 5], maxlen=6)
#clear(self, *args, **kwargs),字面意思
x.clear()
print(x)                            #deque([], maxlen=6)

x=deque(range(5))                   #deque([0, 1, 2, 3, 4])
#copy(self, *args, **kwargs),软复制
y=x
z=x.copy()
print(id(x))                        #2110968289440
print(id(y))                        #2110968289440
print(id(z))                        #2110968288960

x=deque([0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4])
#count(self, value),统计x的数量
print(x.count(4))                   #10

x=deque(range(3))
#extend(self, *args, **kwargs),在deque右侧增加可迭代对象
x.extend(['A','S','d'])
print(x)                            #deque([0, 1, 2, 'A', 'S', 'd'])
#extendleft(self, *args, **kwargs),类似于extend(),但是左边
x.extendleft(['!','@','#'])
print(x)                            #deque(['#', '@', '!', 0, 1, 2, 'A', 'S', 'd'])

#index(self, value, start=None, stop=None)
x = deque('helloword')
print(x.index('o'))             #4
print(x.index('o', 5)) #6
#print(x.index('o', 5, 6))          #找不到报错了(ValueError)

#insert(self, index, p_object),在index位置插入p_object
x = deque('helloword')
x.insert(0, '1')
print(x)                            #deque(['1', 'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'd'])
x.insert(0, ['123'])
print(x)                            #deque([['123'], '1', 'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'd'])

#pop(self, *args, **kwargs),弹出并返回最后一个元素
x=deque('helloword')
print(x.pop())                      #d
print(x)                            #deque(['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r'])

#popleft(self, *args, **kwargs),同pop,但是左边
x=deque('helloword')
print(x.popleft())                  #h
print(x)                            #deque(['e', 'l', 'l', 'o', 'w', 'o', 'r', 'd'])

#remove(self, value),移除第一个出现的value
x = deque('12345')
x.remove('1')
print(x)                            #deque(['2', '3', '4', '5'])

#reverse(self),反转
x = deque('12345')
x.reverse()
print(x)                            #deque(['5', '4', '3', '2', '1'])

#rotate(self, *args, **kwargs),循环向右位移n步,n默认为1
x = deque('12345')
x.rotate()                          #deque(['5', '1', '2', '3', '4'])
x.rotate(3)                         #deque(['2', '3', '4', '5', '1'])
x.rotate(-3)                        #
点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注