How to Use the python dictionary for multi-key and repeated key values (detailed description), python dictionary

Source: Internet
Author: User

How to Use the python dictionary for multi-key and repeated key values (detailed description), python dictionary

Use a dictionary in Python in the following format:

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

The usage format for actually accessing dictionary values is as follows:

dict[key]

Multi-key value

The multi-key value format of the dictionary is as follows:

dict={(ke11,key12):value ,(key21,key22):value ...}

The specific format of actually accessing the value in the dictionary is as follows (take the first key as an example ):

dict[key11,key12]

Or:

dict[(key11,key12)]

The following is an example:

Multi-Value

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

dict={key1:(value1,value2 ..), key2:(value1,value2 ...) ...}

The format of the value in the access dictionary is as follows:

dict[key]

Or

dict[key][index]

Cyclic assignment (emphasis)

The syntax structure is shown in the following example:

Summary:

Through the preceding description, we can know that in the definition of the dictionary, the front and back of the colon (:) are a whole, that is, the front and back of the colon are included by parentheses, when accessing the dictionary value, it is best to put the key in parentheses as a whole.

Multiple key-value pairs with the same key value

That is, in the dictionary, there are at least two members with the same key, but the corresponding values of the key are different. The format is as follows:

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

In this form, the value assigned to the key later becomes the real value of the key.

Use the list and dictionary as the dictionary Value

Format

dict={ key1:(key11:value,key12:value) ,    key2:(key21:value,key22:value)     }

The format of the dictionary to be accessed (take the first key as an example ):

dict[key1][key11]

The actual example is as follows:

The above is all about how to use the multi-key and repeated key values of the python dictionary provided by xiaobian (detailed description). I hope you can support the help house ~

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.