Python dictionary use of multiple key values and duplicate key values (detailed) _python

Source: Internet
Author: User
Tags in python

Use a dictionary in Python in the following format:

dict={key1:value1, Key2;value2 ...}

The following format is used when the dictionary value is actually accessed:

Dict[key]

Multi-key value

The multiple-key form of a dictionary is as follows:

dict={(KE11,KEY12): Value, (KEY21,KEY22): Value ...}

The actual access to the values in the dictionary is shown as follows (take the first key as an example):

DICT[KEY11,KEY12]

Or is:

dict[(KEY11,KEY12)]

Here is the actual example:

Multi-valued

When a key value corresponds to multiple values, the format:

Dict={key1: (Value1,value2 ...), Key2: (Value1,value2 ...) ...}

The format for accessing the values in the dictionary is as follows:

Dict[key]

Or

Dict[key][index]

Circular Assignment (emphasis)

The syntax structure is as shown in the following example

Summarize:

By using the instructions above, you know that in the definition of a dictionary, the colon (:) number is a whole before and after, even if you use parentheses () to include the part before and after the colon, when accessing the dictionary value, it is best to place the key in parentheses as a whole.

Multiple key value pairs with the same key value

In the dictionary, a key with at least two members is the same, but the key corresponds to a different value, in the following format:

dict={key1:value1 
    key1:vaklue2,
    ...}

The value that is assigned to the key later in this formal form becomes the true value of the key.

Use lists, dictionaries as values for dictionaries

Format

dict={Key1: (key11:value,key12:value),
    Key2: (key21:value,key22:value) 
    }

Access dictionary is worth the formatting (take the first key as an example):

DICT[KEY1][KEY11]

The actual example looks like this:

The above is a small series for everyone to bring the Python dictionary multiple key values and duplicate key value of the use of the method (detailed), I hope that we support cloud Habitat Community ~

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.