Python Collections Class

Source: Internet
Author: User

Import Collections
c = Collections. Counter (' Abcdeabcdabcaba ')
Print (c)
Results: Counter ({' A ': 5, ' B ': 4, ' C ': 3, ' d ': 2, ' E ': 1})
Conclusion: The number of occurrences of a statistical string


Import Collections
c = Collections. Counter (' Abcdeabcdabcaba ')
Print (c)

ret = C.most_common (2)
Print (ret)
Results: [(' A ', 5), (' B ', 4)]
Conclusion: The two most frequently occurring strings are counted

Import Collections
c = Collections. Counter (' Abcdeabcdabcaba ')
Print (c)
Print (sorted (c))
Results: [' A ', ' B ', ' C ', ' d ', ' e ']
Conclusion: List all the unique elements

Import Collections
c = Collections. Counter (' Abcdeabcdabcaba ')
Print (c)
For k,v in C.items ():
Print (K,V)
Results:

 Counter ({' A ': 5, ' B ': 4, ' C ': 3, ' d ': 2, ' E ': 1})
A 5
C 3
E 1
D 2
B 4

Conclusion: Count the occurrences of each string

Ordereddict

Import Collections
DIC = collections. Ordereddict ()
dic[' K3 '] = ' v3 '
dic[' k1 '] = ' v1 '
dic[' k2 '] = ' v2 '
Print (DIC)
Results: ordereddict ([' K3 ', ' V3 '), (' K1 ', ' v1 '), (' K2 ', ' V2 ')])
Conclusion: Using lists to maintain an ordered dictionary

Defaultdict
Import Collections
values = [11,22,33,44,55,66,77,88,99]
DIC = collections.defaultdict (list) #设置缺省为list类型
For value in values:
If value <66:
dic[' K1 '].append (value)
Else
dic[' K2 '].append (value)
Print (dic[' K1 ')
Print (dic[' K2 ')

Do not use the default dictionary
values = [11,22,33,44,55,66,77,88,99]
Dic1 = {' K1 ': []}
Dic2 = {' K2 ': []}

For value in values:
If value < 66:
dic1[' K1 '].append (value)
Else
dic2[' K2 '].append (value)
Print (DIC1)
Print (DIC2)
Results:

  {' K1 ': [11, 22, 33, 44, 55]}
{' K2 ': [66, 77, 88, 99]}

Conclusion: The value of DIC1,DIC2 must be established as a list empty table

Namedtuple (named Ganso)

Import Collections

Test = collections.namedtuple (' Test ', (' x ', ' y ', ' z '))
obj = Test (11,22,33)
Print (obj.x)
Results: 11
Conclusion: For the meta-ancestor, access to the data can only be accessed by subscript, but for a named Ganso, you can construct a new class (such as test) and then invoke the method to implement

Bidirectional queue

Import Collections
dq = collections.deque ()

One-way queues
Import queue
Q = queue. Queue ()
Q.put ("123")
R = Q.get ()
Print (R)




Python Collections Class

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.