Python Small white-day3 collections series

Source: Internet
Author: User
Tags for in range iterable

First, counter (Counter)

Counter is a supplement to the dictionary type that is used to track the number of occurrences of a value.

Note: Because counter inherits from the Dict class, it has all the functions of the Dict class

1 Import Collections 2 C1 = collections. Counter ('skdhflsdkngsnlknvdlfb')3print(C1)
Counter

1, __missing__ (self, key) for non-existent elements, the return counter is 0

Import COLLECTIONSC1 = collections. Counter (' SKDHFLSDKNGSNLKNVDLFB ') print (c1.__missing__ (' Q '))

2. Most_common (self, n=none) displays the first n elements and counters

1 Import Collections 2 C1 = collections. Counter ('skdhflsdkngsnlknvdlfb')3print(C1) 4 Print (C1.most_common (4))
Most_common

3. All elements in the elements (self) counter, note: Here is not a collection of all elements, but an iterator that contains all the collection of elements

Import COLLECTIONSC1 = collections. Counter (' SKDHFLSDKNGSNLKNVDLFB ') item = list (C1.elements ()) print (item)

4, Update (self, Iterable=none, **kwds) updated counter, in fact, is increased; if not, then new, if any, add a

1 ImportCollections2c = Collections. Counter ('which')3 Print(c)4C.update ('Witch')5 Print(c)6D = Collections. Counter ('Watch')7 C.update (d)8 Print(c)
Update

5, Subtract (self, iterable=none, **kwds) subtraction, the number of each element in the original counter minus the number of elements added after

1 ImportCollections2c = Collections. Counter ('which')3 Print(c)4C.subtract ('Witch')5 Print(c)6D = Collections. Counter ('Watch')7 C.subtract (d)8 Print(c)
Subtract

Second, ordered dictionary (ordereddict)

Orderddict is a supplement to the dictionary type, and he remembers the order in which the dictionary elements were added

1 ImportCollections2n \collections. Ordereddict ()3o['K1'] ='v1'4o['K2'] ='v2'5o['K3'] ='v3'6 Print(o)7 Print(o)8 Print(o)9 Print(o)
orderedict

The remaining functions are equivalent to dictionaries

Third, default dictionary (defaultdict)

Defaultdict is a supplement to the type of dictionary, which defaults to a type for the value of the dictionary.

1 Import Collections 2 li = [11,22,33]3 ded = collections.defaultdict (list)4 for In  li:5     ded['K1'].append (i)6  Print(ded)
defaultdict

Four, can be named tuple (namedtuple)

Depending on nametuple, you can create a type that contains all the functions of a tuple and other features.

1 ImportCollections2person = collections.namedtuple (' Person','Name Age gender')3 Print(type (person))4Hetan = person (name ='Hetan', age = 26,gender ='male')5 Print(hetan[0])6 Print('{Name} is {age} years-old {gender}'. Format (name = Hetan[0],age = Hetan[1],gender = hetan[2]))
Namedtuple

Five, two-way queue (deque)

A thread-safe bidirectional queue

1 ImportQueue2Q =Queue.deque ()3  forIinch[1,2,3,4,5]:4 q.append (i)5 Print(q)6Q.appendleft (6)7 Print(q)8 Q.popleft ()9 Print(q)TenQ.rotate (2) One Print(q) ALi = [7,8,9,10] - Q.extendleft (LI) - Print(q)
deque

1, Appendleft (n) Add elements from the left like queue, n represents the added element

2. Popleft () Removes the element from the left side of the queue and returns the delete value

3, rotate (n) rotation queue, the default value is 1, starting from the right to rotate, negative values for the left rotation, n means starting from the first element of the queue, n counting from 1

4, Extendleft (n) extends the queue from the left, N represents the extended queue

Note: Since there are two-way queues, there is also a single queue (FIFO)

1 Import Queue 2 q = queue. Queue ()3 for in [1,2,3,4,5]:4    q.put (i)  5 for in range (5):6     print(Q.get ())
Queue

Python Small white-day3 collections series

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.