Python Dictionary method

Source: Internet
Author: User

0x01 Clear

The clear method clears all items in the dictionary. This is an in-place operation with no return value

1>>> d = {}2>>> d['name'] ='Gumby'3>>> d[' Age'] = 134>>>D5{' Age': 13,'name':'Gumby'}6>>> value =D.clear7>>>Printvalue8<built-inchMethod clear of Dict object at 0x7f8f905c37f8>

0x02 get

Get (key[, default])

The Get method is a more permissive way to access dictionary entries. In general, if you attempt to access an item that does not exist in the dictionary, an error occurs, and a get does not, and you can customize the default value when the access item does not exist.

1>>> d = {}2>>> d['name']3 Traceback (most recent):4File"<stdin>", Line 1,inch<module>5Keyerror:'name'6>>>PrintD.get ('name')7 None8>>>PrintD.get ('name','Default_value')9Default_value

The Get method can also be used to do some statistics, such as counting the number of each character

1 #!/usr/bin/env python2 #Coding=utf-83 4str =" "5 aaaaaaaaaaaaaa6 bbbbbbbbbbbbb7 CCCCCCCCCCCCCCCC8 dddddddddddddd9 33333333333Ten 44444444444 One `````````````` A " " -  -Counts = {} the  -  forCinchStr: -COUNTS[C] = Counts.get (c, 0) + 1 -  + Printcounts -  + """ A {' A ': +, ' ': +, ' C ': +, ' B ': +, ' d ': +, ' \ n ': 8, ' 3 ': One, ' 4 ': one} at """

Python Dictionary method

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.