Python (3)-Queue

Source: Internet
Author: User

Queues are divided into two-way queues and one-way queues:

For two-way queues, import is also required first collections

Create a queue

Import collections>>> d = collections.deque ()>>> type (d)<class ' Collections.deque '>

Adding elements to the queue

>>> D.appendleft ("1")#Add from left>>>Print(d) deque (['1'])>>> D.append ('2')#Add from right>>>Print(d) deque (['1','2'])

Methods in a bidirectional queue

>>> D.append ('2')>>>Print(d) deque (['1','2'])>>> D.append ('2')>>>Print(d) deque (['1','2','2'])#Count>>> D.count ('2')>>> D.extend ([' -',' -'])#Extended>>>Print(d) deque (['1','2','2',' -',' -'])>>> D.extendleft ([' One',' A'])#extend from left>>>Print(d) deque ([' A',' One','1','2','2',' -',' -'])#Note The order, the queue is FIFO>>> D.pop ()#fetch data from the right' -'>>>D.pop ()' -'>>>>>> D.popleft ()#fetch data from the left' A'>>>D.popleft ()' One'>>>>>>Print(d) deque (['1','2','2'])>>> D.remove ("2")#Delete data from a queue>>>Print(d) deque (['1','2'])>>> D.append ([" -"," -"])#Add a list to the queue>>>Print(d) deque (['1','2', [' -',' -']]) >>> d.rotate (1)#Put the last one in front .>>>Print(d) deque ([[' -',' -'],'1','2'])>>> D.rotate (2)#Put the last two in front of you.>>>Print(d) deque (['1','2', [' -',' -']])>>> D.reverse ()#reversal>>>Print(d) deque ([[' -',' -'],'2','1'])

One-way queues:

One-way queue requires import queue

Import queue>>> q = queue. Queue ()    # creates a one-way queue >>> type (q)<class'queue. Queue'>

Method of one-way queue

>>> Q.put ("a")#placing elements in a queue>>> Q.put (["b","C"])>>>Print(q)<queue. Queue object at 0x000000000309de80>>>> q.qsize ()#number of elements in the queue2 >>> Q.get ()#take the element out, FIFO, and when the element is finished, the queue waits until an element is put'a'>>>q.get () ['b','C']>>>Q.get ()>>> q =queue. Queue ()>>> Q.empty ()#whether the queue is emptyTrue>>> q = queue. Queue (maxsize = 3)#define the maximum length of a queue>>> Q.put (1)>>> Q.put (2)>>> Q.put (3)>>> Q.put (4)#when the put element exceeds the maximum length, the queue waits until the data unit is vacated

Python (3)-Queue

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.