Detailed Explanation of Python Dictionary

  • 2021-12-05 06:49:11
  • OfStack

What is a dictionary? Create a dictionary 1. Direct assignment 2. Use dict () function to create an empty dictionary 3. Get dictionary elements 4. Only 1 key, if there are two or more identical keys in the dictionary, take the last summary by default: dictionary addition dictionary change dictionary deletion 1. del method 2. pop method 3. popitme method 4. clear method dictionary look-up dictionary value 1. keys method 2. values method 3. items method dictionary copy traversal dictionary summary

What is a dictionary?

Dictionary is an unordered data set in Python, and the dictionary uses key-value pairs for storage.


Dict = {'key':'value'}

Create a dictionary

Assign directly, using curly braces {} Create an empty dictionary using the Dict function

1. Direct assignment


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
print(Dict)
print(type(Dict))
 
>>> {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
>>> <class 'dict'>

2. Create an empty dictionary using the dict () function


Dict = dict()
print(Dict)
print(type(Dict))
 
>>> {}
>>> <class 'dict'>

3. Get dictionary elements


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
print(Dict[' Chacha Division '])
 
>>>  Lu Zhidao 

4. The only key is 1. If there are two or more identical keys in the dictionary, the last key is taken by default


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue ', ' Division of Punishing Evil ': ' Li Gui '}
print(Dict[' Division of Punishing Evil '])
 
>>>  Li Gui 

Summary:

The dictionary consists of a key (key) and a value (value). Creation can use curly braces or the dict function, with commas separating elements and colons separating key values. Note: The key (key) must be of immutable type, such as number or string, while the value (value) is optional. The dictionary element is retrieved using the key (key). The keys are only 1, so try not to be the same.

An increase in dictionaries


Dict[ Key ] = ' Value '

Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
Dict[' Residents '] = ' Dream Lion '
print(Dict)
 
>>> {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue ', ' Residents ': ' Dream Lion '}

Dictionary revision

方法 描述
直接修改 直接修改,方法跟增差不多
update 将字典元素添加到另1个字典中,如果被添加字典已有该元素则修改,否则增加

Dict[ Key ] = ' Value '

Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
#  Modify the corresponding value if the key already exists in the dictionary , Otherwise increase 
Dict[' Division of Punishing Evil '] = ' Dream Lion '
print(Dict)
 
>>> {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Dream Lion ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}

2. Use update


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
Dict2 = {' Heavenly Master ': ' Zhong Kui ', ' Division of Punishing Evil ': ' Tian Shi Zhong Kui '}
Dict.update(Dict2)
print(Dict)
 
>>> {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Tian Shi Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue ', ' Heavenly Master ': ' Zhong Kui '}

Deletion of dictionary

方法 描述
del 删除某个元素,或删除整个字典
pop 删除某个元素
popitme 删除字典中最后1个元素
clear 清空字典

1. del method


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
print(Dict)
print(type(Dict))
 
>>> {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
>>> <class 'dict'>
0

2. pop method


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
print(Dict)
print(type(Dict))
 
>>> {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
>>> <class 'dict'>
1

3. popitme method


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
print(Dict)
print(type(Dict))
 
>>> {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
>>> <class 'dict'>
2

4. clear method


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
Dict.clear()
print(Dict)
 
#  Because the dictionary has been emptied 
>>> {}

Dictionary search

方法 描述
in 判断元素是否在字典中,返回值为 Bool
not in 与 in 相反

Because this is a general method, which has been said many times before, I will skip it now.

Dictionary value

方法 描述
keys

获取字典中的键,返回1个迭代

values 获取字典中的值,返回1个迭代
items 获取字典的键值,返回1个迭代

1. keys method


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
print(Dict)
print(type(Dict))
 
>>> {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
>>> <class 'dict'>
4

2. values method


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
print(Dict)
print(type(Dict))
 
>>> {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
>>> <class 'dict'>
5

3. items method


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
print(Dict)
print(type(Dict))
 
>>> {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
>>> <class 'dict'>
6

Dictionary copying

Use the copy function as usual


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
print(Dict)
print(type(Dict))
 
>>> {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
>>> <class 'dict'>
7

Traversal dictionary

Traversing key Traversing value Traversing item Traversing key-value

1. Traversing key


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
for i in Dict.keys():
    print(i)
 
 
>>>  Rewarding the good 
     Division of Punishing Evil 
     Chacha Division 
     Department of Yin Law 

2. Traverse value


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
print(Dict)
print(type(Dict))
 
>>> {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
>>> <class 'dict'>
9

3. Traversing item


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
for i in Dict.items():
    print(i)
 
>>> (' Rewarding the good ', ' Wei Zhi ')
    (' Division of Punishing Evil ', ' Zhong Kui ')
    (' Chacha Division ', ' Lu Zhidao ')
    (' Department of Yin Law ', ' Cui Jue ')

4. Traversing key and value


Dict = {' Rewarding the good ': ' Wei Zhi ', ' Division of Punishing Evil ': ' Zhong Kui ', ' Chacha Division ': ' Lu Zhidao ', ' Department of Yin Law ': ' Cui Jue '}
for key, value in Dict.items():
    print(key, value)
 
>>>  Rewarding the good   Wei Zhi 
     Division of Punishing Evil   Zhong Kui 
     Chacha Division   Lu Zhidao 
     Department of Yin Law   Cui Jue 

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: