Python crawler (ii)--Learn about Deque

Source: Internet
Author: User
Tags iterable

Queue-deque
With the basics of the above section, of course you need to fully master all the methods in the previous section, because the methods in the previous section, in the following tutorial
will be used over and over again.
If you do not remember, please return to the previous section.
In this section we want to understand a queue--deque. In the following crawler basis, we also want to use deque repeatedly, to complete the URL
Out of the queue.

With a basic understanding of deque, we began to learn more about him.

Colloections.deque ([Iterable[,maxlen]])
Initializes a new Deque object from left to right, and generates an empty deque if iterable is not given.
Deque is a generic for stack (stacks) and queue (queues), deque supports thread safety. It is efficient to deposit or remove from both sides.

Although the list object supports the same operation, it is inefficient to use the list to complete the queue function because list is used at the head of the team
Both pop () and insert () are less efficient.

If MaxLen is not defined, then deque is any length, otherwise deque can only be a specified length

What are the methods of Deque objects?

1.1:append (x)
add element x to the far right of deque
Instance:
From collections Import Deque
A=deque ()
For I in range (10):
A.append (i)
Print (a)
The result of the output is:
Deque ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

1.2:appendleft (x)
add element x to the leftmost side of deque
Instance:
From collections Import Deque
A=deque ()
For I in range (10):
A.appendleft (i)
Print (a)
The result of the output is:
Deque ([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

1.3:clear ()
Delete all elements of deque
From collections Import Deque
A=deque ()
For I in range (10):
A.appendleft (i)
Print (a)
A.clear ()
Print (a)
The result of the output is:
Deque ([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
Deque ([])

1.4:count ()
Count the number of deque elements
From collections Import Deque
A = Deque ()
For x in range (10):
A.append (x)
A.appendleft (1)
Print (A.count (1))
The result of the output is: 2

1.5:extend (iterable)/extendleft (iterable)
Add the elements from the Iterbale to the deque and add them to the right by default,
From collections Import Deque

A=deque ()
l=[1,2,3,4]

For x in range (10):
A.append (x)
A.extend (L)
Print (a)
The result of the output is:
Deque ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4])
[Finished in 0.2s]

1.6:pop ()/popleft ()
Delete the element at the end, the Pop () method does not work like a list object can use Pop (2)
Instance:
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)
The result of the output is:
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)
Delete the first occurrence of value, if no value is an error
Instance:
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)
The result of the output is:
Deque ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4])
Traceback (most recent):
Deque ([0, 1, 2, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4])
File "D:\Python\xode\try.py", line ten, in <module>
A.remove (11)
ValueError:deque.remove (x): X not in Deque

1.8:reverse ()
All elements are reversed and return none
Instance:
From collections Import Deque
A=deque ()
For x in range (10):
A.append (x)
Print (a)
Print (A.reverse ())
Print (a)
The result of the output is:
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)
Move the left and right elements to the left by N steps, and if n is a negative number then move to n=1 equivalent to:
A.appendleft (D.pop ())

From collections Import Deque
A=deque ()
For x in range (10):
A.append (x)
Print (a)
A.rotate ()
Print (a)
The result of the output is:
Deque ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Deque ([9, 0, 1, 2, 3, 4, 5, 6, 7, 8])

Finally, let's look at some examples from official documents:
>>> 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):
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 '])


Zhongzhiyuan Nanjing 904727147, Jiangsu

Python crawler (ii)--Learn about Deque

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.