python爬虫(二)–了解deque

688次阅读
没有评论
python爬虫(二)--了解deque

队列-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:Pythonxodetry.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

 

     

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python教程2022-11-01发表,共计3840字。
新手QQ群:570568346,欢迎进群讨论 Python51学习