Python Learning Notes (10)--Combined data type (dictionary type)

Source: Internet
Author: User

Understanding Mappings:

A map is the correspondence of a key (index) and a value (data). A dictionary is a collection of key-value pairs that are unordered between key values. The curly braces denote {}, and Dict () are created, and the key-value pairs are represented by a colon:.

{key: Value, key: Value, key: Value}
>>> d={"China":"Beijing","United States":"Washington","United Kingdom":"London"}>>>d{'China':'Beijing','United States':'Washington','United Kingdom':'London'}>>> d["China"]'Beijing'>>> de={}" "define an empty dictionary" ">>>type (DE)<class'Dict'>

{} is used to generate an empty dictionary type, and if the collection type is empty, a set function is required and cannot be generated using {}.

How to handle dictionary types:

Del D[k] Delete the data value corresponding to K in dictionary D.

K in D whether a key is in dictionary D

D.keys () returns all key information in dictionary D

D.values () Returns all value information in dictionary D

D.items () returns information for all key-value pairs in dictionary D

>>>"China" inchdtrue>>>D.keys () Dict_keys (['China','United States','United Kingdom'])  >>>d.values<built-inchMethod values of DictObjectAt0x0000000003011828>>>>d.values () dict_values (['Beijing','Washington','London'])/returned is not a list type and can be traversed in a for-in way, but not as a list type>>>D.items () dict_items ([('China','Beijing'), ('United States','Washington'), ('United Kingdom','London')])>>> del d["China"]>>>d{'United States':'Washington','United Kingdom':'London'}

D.get (k,<default>) key k exists, the corresponding value is returned, the default value is not returned

D.pop (k,<default>) key k is present, the corresponding value is removed, the corresponding key-value pair is deleted, and the default value is not returned

D.popitem () Randomly extracts a key-value pair from dictionary D and returns it as a tuple

D.clear () Removes all key-value pairs.

Len (d) Returns the number of elements in D

>>> d={}>>> d["type"]=2>>> d[" value "] =>>> d{'type'2'value  ' + '

The application scenario of a dictionary: the expression of a map

Python Learning Notes (10)--Combined data type (dictionary type)

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.