标签:
队列-deque
有了上面一节的基础,当然你需要完全掌握上一节的所有方法,因为上一节的方法,在下面的教程中
会反复的用到。
如果你没有记住,请你返回上一节。
这一节我们要了解一种队列--deque。在下面的爬虫基础中,我们也要反复的使用deque,来完成网址
的出队入队。
有了对deque基本的认识,我们开始进一步的学习了解他。
colloections.deque([iterable[,maxlen]])
从左到右初始化一个新的deque对象,如果iterable没有给出,那么产生一个空的deque.
deque是栈(stacks)和队列(queues)的泛指,deque支持线程安全。从两边进行存入或者取出是高效率的
尽管list对象支持相同的操作,但是list用来完成队列功能其实是低效率的,因为list在队首使用
pop()和insert()都是效率比较低的。
如果maxlen没有定义,那么deque可是是任意长度,否则deque只能是指定长度的
deque对象有哪些方法?
1.1:append(x)
向deque的最右边添加元素x
实例:
from collections import deque
a=deque()
for i in range(10):
a.append(i)
print(a)
输出的结果是:
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
1.2:appendleft(x)
向deque的最左边添加元素x
实例:
from collections import deque
a=deque()
for i in range(10):
a.appendleft(i)
print(a)
输出的结果是:
deque([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
1.3:clear()
删除deque的所有元素
from collections import deque
a=deque()
for i in range(10):
a.appendleft(i)
print(a)
a.clear()
print(a)
输出的结果是:
deque([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
deque([])
1.4:count()
统计deque元素个数
from collections import deque
a = deque()
for x in range(10):
a.append(x)
a.appendleft(1)
print(a.count(1))
输出的结果是:2
1.5:extend(iterable)/extendleft(iterable)
把iterbale中的元素都添加到deque中,默认从右边开始添加,
from collections import deque
a=deque()
l=[1,2,3,4]
for x in range(10):
a.append(x)
a.extend(l)
print(a)
输出的结果是:
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4])
[Finished in 0.2s]
1.6:pop()/popleft()
删除末尾的元素,pop()方法并不能像list对象那样可以使用pop(2)
实例:
from collections import deque
a=deque()
l=[1,2,3,4]
for x in range(10):
a.append(x)
a.extend(l)
print(a)
a.pop()
print(a)
输出的结果是:
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4])
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3])
1.7:remove(value)
删除第一个出现的value,如果没有value报错
实例:
from collections import deque
a=deque()
l=[1,2,3,4]
for x in range(10):
a.append(x)
a.extend(l)
print(a)
a.remove(3)
print(a)
a.remove(11)
print(a)
输出的结果是:
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4])
Traceback (most recent call last):
deque([0, 1, 2, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4])
File "D:\Python\xode\try.py", line 10, in <module>
a.remove(11)
ValueError: deque.remove(x): x not in deque
1.8:reverse()
所有元素反转,并返回None
实例:
from collections import deque
a=deque()
for x in range(10):
a.append(x)
print(a)
print(a.reverse())
print(a)
输出的结果是:
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
None
deque([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
1.9:rotate(n)
将左右元素向右移动n步,如果n是负数那么向左移动,n=1相当于:
a.appendleft(d.pop())
from collections import deque
a=deque()
for x in range(10):
a.append(x)
print(a)
a.rotate()
print(a)
输出的结果是:
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
deque([9, 0, 1, 2, 3, 4, 5, 6, 7, 8])
最后再看一些官方文档给出的例子:
>>> from collections import deque
>>> d = deque(‘ghi‘) # make a new deque with three items
>>> for elem in d: # iterate over the deque‘s elements
... print(elem.upper())
G
H
I
>>> d.append(‘j‘) # add a new entry to the right side
>>> d.appendleft(‘f‘) # add a new entry to the left side
>>> d # show the representation of the deque
deque([‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘])
>>> d.pop() # return and remove the rightmost item
‘j‘
>>> d.popleft() # return and remove the leftmost item
‘f‘
>>> list(d) # list the contents of the deque
[‘g‘, ‘h‘, ‘i‘]
>>> d[0] # peek at leftmost item
‘g‘
>>> d[-1] # peek at rightmost item
‘i‘
>>> list(reversed(d)) # list the contents of a deque in reverse
[‘i‘, ‘h‘, ‘g‘]
>>> ‘h‘ in d # search the deque
True
>>> d.extend(‘jkl‘) # add multiple elements at once
>>> d
deque([‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘])
>>> d.rotate(1) # right rotation
>>> d
deque([‘l‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘])
>>> d.rotate(-1) # left rotation
>>> d
deque([‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘])
>>> deque(reversed(d)) # make a new deque in reverse order
deque([‘l‘, ‘k‘, ‘j‘, ‘i‘, ‘h‘, ‘g‘])
>>> d.clear() # empty the deque
>>> d.pop() # cannot pop from an empty deque
Traceback (most recent call last):
File "<pyshell#6>", line 1, in -toplevel-
d.pop()
IndexError: pop from an empty deque
>>> d.extendleft(‘abc‘) # extendleft() reverses the input order
>>> d
deque([‘c‘, ‘b‘, ‘a‘])
钟志远 江苏南京 904727147
标签:
原文地址:http://my.oschina.net/u/2391943/blog/486498