Use a dictionary in Python, the format is as follows:
1 |
|
The format used when actually accessing dictionary values is as follows:
1 |
|
Multi-key
The multi-key form of the dictionary is as follows:
1 |
|
The specific form when actually accessing the value in the dictionary is as follows (take the first key as an example):
1 |
|
or:
1 |
|
The following is a practical example:
Multivalued
When a key value corresponds to multiple values, the format:
1 |
|
The format for accessing the values in the dictionary is as follows:
1 |
|
or
1 |
|
Loop assignment (emphasis)
The syntax structure is shown in the following example
to sum up:
Through the above description, we can know that in the definition of the dictionary, the colon (:) before and after the sign is a whole respectively, that is, use parentheses () to include the parts before and after the colon respectively. When accessing the dictionary value, it is best to put the key Become a whole in parentheses.
Multiple key-value pairs with the same key-value
That is, in the dictionary, there are at least two members whose keys are the same, but the values corresponding to the keys are different. The format is as follows:
1 2 3 |
|
In this form, the value assigned to the key later becomes the true value of the key.
Use lists and dictionaries as dictionary values
format
1 2 3 |
|
Access dictionary value format (take the first key as an example):
1 |
|
The actual example is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
Print result:
China
Option 1 list as the value of dict allows repeating
{1: [2, 2]}
Get the value
[2, 2] and
delete the value, leaving an empty list
{1: []} to
check if there is another value
[]
方案二 使用子字典作为dict的值 值不允许重复
{1: {2: 22, 3: 33}}
获取值
[2, 3]
删除值,会留下一个空列表
{1: {}}
检查是否还有一个值
{}
方案三 使用set作为dict的值 值不允许重复
{1: set([2, 3])}
获取值
[2, 3]
删除值,会留下一个空列表
{1: set([])}
检查是否还有一个值
set([])