Python container data type-collections

Source: Internet
Author: User
#  ! /Usr/bin/Python  #  Coding = UTF-8  #  Http://docs.python.org/library/collections.html  #  Count object only 2.7  From Collections Import  Counter  #  Count the number of occurrences of letters Counter ( '  Hello World '  ) Counter ([  '  Red  ' , '  Blue  ' , '  Red  ' , '  Green  ' , '  Blue  ' ,'  Blue  '  ])  #  A value smaller than or equal to 0 is ignored. C = counter (A = 4, B = 2, c = 0, D =-2 ) List (C. Elements ())  #  Take the first three letters Counter ( '  Hello World  ' ). Most_common (3 )  #  Heap  From Collections Import  Dequed = Deque ( '  ABC  '  ) D. append (  '  D  '  ) D. Pop ()  #  Post-in-first-out D. popleft () #  First in first out  #  Returns the last n lines of text. Deque (open (filename), n)  #  Defaultdict  From Collections Import  Defaultdict  #  Use List to initialize a dict D = Defaultdict (list) d [  "  Yellow  " ]. Append (1 ) D [  "  Red  " ]. Append (2 ) D [  "  Yellow  " ]. Append (3 )  Print D. Items () #  [('Red', [2]), ('yellow', [1, 3])]  #  Use int to initiate a dict D = Defaultdict (INT) d [  "  Yellow  " ] + = 1 D [ "  Red  " ] + = 2 D [  "  Yellow  " ] + = 3 Print D. Items () #  [('Red', 2), ('yellow', 4)] 
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.