Python devops Development Road 02

Source: Internet
Author: User
Tags shallow copy

The end of the matter more, in the company busy into a dog, rarely put blog updated under

The main content of this chapter is the introduction of the collections module (queues, counters, default dictionaries, ordered dictionaries, named Tuples, and a simple introduction to deep-and-shallow copies!) )

Collections Module Collection Counter counter: counter  is the function of the parent class of the inherited dictionary, so many methods are derived from the dictionary. Its function is to calculate the number of occurrences of each element >>> import collections>>>ret=collections. Counter ("Asdfkljnklxcjiovjkmq12;3ljh4n34") >>> print (ret) Counter ({' J ': 4,  ' K ':  3,   ' l ': 3,  ' n ': 2,  ' 3 ': 2,  ' 4 ': 2,  ' F ': 1,  ' H ':  1,   ' m ': 1,  ' d ': 1,  ' V ': 1,  ' a ': 1,  ' o ': 1,  ' i ':  1,   ' 1 ': 1,  ' s ': 1,  '; ': 1,  ' x ': 1,  ' 2 ': 1,  ' C ':  1,   ' Q ':  1}) obj=collections. Counter ([1,2,3,4,5]) >>> objcounter ({1: 1, 2: 1, 3: 1, 4: 1, &NBSP;5:&NBSP;1}) >>> obj.subtract ([1,2,10])   #会把不在列表里面的元素进行统计, found not once-11 times. >>> objcounter ({3: 1, 4: 1, 5: 1, 1: 0, 2: 0,  10:&NBSP;-1})      default dictionary (same inheritance dictionary thisClass), through this module, we can easily insert a list in the dictionary, using a list of methods append elements into the list: >>> dic=collections.defaultdict (list) >> > dicdefaultdict (<class  ' list ' >, {}) >>> type (DIC) <class  ' Collections.defaultdict ' >>>> dir (DIC) [' __class__ ',  ' __contains__ ',  ' __copy__ ',   ' __delattr__ ',  ' __delitem__ ',  ' __dir__ ',  ' __doc__ ',  ' __eq__ ',  ' __format__ ',   ' __ge__ ',  ' __getattribute__ ',  ' __getitem__ ',  ' __gt__ ',  ' __hash__ ',  ' __init__ ',   ' __iter__ ',  ' __le__ ',  ' __len__ ',  ' __lt__ ', ' __missing__ ',  ' __ne__ ',  ' __new__ ',   ' __reduce__ ',  ' __reduce_ex__ ',  ' __repr__ ',  ' __setattr__ ',  ' __setitem__ ',  ' __ sizeof__ ',  ' __str__ ',  ' __subclasshook__ ',  ' clear ',  ' copy ',  ' default_factory ',  ' Fromkeys ',  ' get ',  ' items ',  ' Keys ',  ' Pop ',  ' popitem ',  ' setdefault ',  ' Update ' ,  ' Values ']>>> d ic[' K1 '].append (' Jiafa ') >>> dicdefaultdict (<class  ' list ' >, {' K1 ':  [' Jiafa ']}) >>> dicdefaultdict (<class  ' list ' >, {' K1 ':  [' Jiafa ']}) >>>  DIC (' K2 '). Append (' SDFXCV ') traceback  (most recent call last):  file  "< Stdin> ", line 1, in <module>typeerror:  ' collections.defaultdict '  object  is not callable>>> dic[' K2 '].append (' SDFXCV ') >>> dicdefaultdict ( <class  ' list ' >, {' K1 ':  [' Jiafa '],  ' K2 ':  [' SDFXCV ']})    Ordered dictionary (Common dictionary we can all see the dictionary is Disorderly order!) ): >>>dicorder=collections. Ordereddict () >>> dicorder[' K1 ']= ' v1 ' >>> dicorder[' K2 ']= ' v2 ' >>>  dicorder[' K3 ']= ' v3 ' >>> dicorderordereddict ([(' K1 ',  ' v1 '),  (' K2 ',  ' v2 '),  (' K3 ',  ' v3 ')] >>> dicorderordereddict ([(' K1 ',  ' v1 '),  (' K2 ', &NBSP; ' V2 '),  (' K3 ',  ' v3 ')] >>> dicorderordereddict ([' K1 ',  ' v1 '),  (' K2 ',  ' v2 ') ,  (' K3 ',  ' v3 ')]) you can see that the order of the dictionaries is never messy, regardless of the number of print times!     named tuples: >>>mytupleclass=collections.namedtuple ("Mytupleclass", [' X ', ' y ', ' z '])         #创建一个类, the x, Y, Z equals >>> obj=mytupleclass (11,22,33)     #创建了一个对象, 11=x,22=y , 33=z.>>> objmytupleclass (x=11, y=22, z=33) >>> obj.x11>>>  obj.y22     Queue:Two-way queue: 12-terminal simultaneous start 2 based on thread-safe, wired lock mechanism. bidirectional Queue>>> import collections>>> q=collections.deque () >>> q.append (1) >>> Q.append (2) >>> q>>> q.append (' asdf ') >>> qdeque ([1, 2, ' asdf ']) >>> q.append (' 1LX ') # There are also extend ways to do it,>>> d.rotate () >>> Ddeque ([5, 1, 3, 1, 2, 3, ten, 4]) >>> d.rotate () >>> DD Eque ([4, 5, 1, 3, 1, 2, 3, ten]) >>>>>> qdeque ([1, 2, ' asdf ', ' 1LX ']) deque ([1, 2]) >>> q.pop () ' 1LX ' >>> qdeque ([1, 2, ' asdf ']) >>> x=q.pop () >>> print (x) asdf>>> a=[1,2,34,5]>> > C=a.pop () >>> print (c) 5 ========== # Queue one-way queues==========import Queue
Myqueue = queue.queue (maxsize = 10)

The Queue.queue class is a synchronous implementation of a queue. The queue length can be unlimited or limited. The queue length can be set through the optional parameter maxsize of the queue's constructor. If MaxSize is less than 1, it means that the queue length is infinite.

Put a value in the queue

Myqueue.put (10)
The put () method of the call queue object inserts an item at the end of the team. Put () has two parameters, the first item is required, the value of the inserted item, the second block is an optional parameter, and the default is 1. If the queue is currently empty and the Block is the 1,put () method, the calling thread pauses until a data cell is vacated. If the block is the 0,put method, the full exception is thrown.

Take a value out of the queue

Myqueue.get ()

The Get () method of the call queue object is removed from the team header and returns an item. The optional parameter is block, which is true by default. If the queue is empty and the Block is True,get (), the calling thread is paused until a project is available. If the queue is empty and the block is false, the queue throws an empty exception.


There are three types of queues in the Python queue module:
1, the Python queue module FIFO queuing first-out.
2. LIFO is similar to heap. That is, advanced back out.
3, there is a priority queue level lower the more first out.

There are three constructors for each of the three queues:
1, Class Queue.queue (maxsize) FIFO
2, Class Queue.lifoqueue (maxsize) LIFO
3, Class Queue.priorityqueue (maxsize) Priority queue

Describe the common methods in this package:

Queue.qsize () returns the size of the queue
Queue.empty () returns True if the queue is empty, and vice versa false
Queue.full () returns True if the queue is full, otherwise false
Queue.full corresponds to maxsize size
Queue.get ([block[, timeout]]) Get queue, timeout wait time
Queue.get_nowait () quite queue.get (False)
Non-blocking Queue.put (item) write queue, timeout wait time
Queue.put_nowait (item) quite Queue.put (item, False)
Queue.task_done () After completing a work, the Queue.task_done () function sends a signal to the queue that the task has completed
Queue.join () actually means waiting for the queue to be empty before performing another operation >>> import queue>>> a=queue. Queue () >>> a<queue. Queue object at 0x0000009a888b3a58>>>> a.put (1) >>> a.put (2) >>> a.put (3) >>> A.qsize () 4>>> a.get () 1>>> a.get () 1>>> a.get () 2>>> a.get () 3>>> a.qsize ( ) 0>>> a.get () #---Stuck there, waiting for the other end of the queue to enter two ways to access a queue:1 FIFO (first in first out). 2 stacks, similar to magazines, LIFO. Depth Copy:For strings and numbers, the depth of copy and assignment is meaningless. Because it always points to the same piece of memory address. String, the copy method inside the tuple list dictionary is actually called the Copy.copy () method (shallow copy) deep copy: Copy.deepcopy ()

Python devops Development Road 02

Related Article

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.