List of articles

    • a) key The judgment of the
    • b) Deletion of dictionary elements
    • c) The addition of dictionary elements
    • d) How to get the dictionary view
    • 1) What is a dictionary 、
    • 2) Dictionary creation
    • 3) The acquisition of dictionary elements
    • 1. An overview of the dictionary
    • 2.3 Look up the dictionary
    • 3. Element traversal of the dictionary
    • 4. Dictionary generative
    • 5. summary


1. An overview of the dictionary

1) What is a dictionary 、

  • Python One of the data structures built into , Like a list, it's a variable sequence
  • Store data in key value pairs , A dictionary is an unordered sequence
    sorce={‘ Internet veteran Xin ’:34, ‘itlaoxin’:50,' Gao Shenghan ‘’:30}
     Insert picture description here

The realization principle of Dictionary :
It's like looking up a dictionary ,Python Chinese first basis key lookup value Where it is

2) Dictionary creation

The most common way :

  • Use curly braces to create
    sorce={‘zhangsan’:100,‘itlaoxin’:55}
  • Use built-in functions dict()
  • dict(name=' Internet veteran Xin ’,age=40)
    Case study :
#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38'''
Dictionary creation
'''# Use {} establish scores={' Internet veteran Xin ':100,' Gao Shenghan ':130}print(scores,type(scores))## Use dict() establish scores2=dict(name=' Internet veteran Xin ',score=150)print(scores2)## Create an empty dictionary d={}print(d)

 Insert picture description here

3) The acquisition of dictionary elements

There are two ways to take values from a dictionary , One is direct use [] Value , such as scores[‘ Internet veteran Xin ’]
The second way : get() such as : scorce.get(‘ Internet veteran Xin ’)

Be careful :

  • If you use... In the dictionary [] The value is not specified key, Will report a mistake ,keyerror abnormal
  • get() Methods the values , If not specified in the dictionary key, No errors are reported , Will only return none, You can set the default value, In order to make it key non-existent

Case study :

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38'''
Dictionary creation
'''# Use {} establish scores={' Internet veteran Xin ':100,' Gao Shenghan ':130}print(scores[' Internet veteran Xin '])print(scores[' Little high ']) #keyError  Report errors print(scores.get(' Internet veteran Xin '))print(scores.get(' Little high ') ## The return value is none

2.3 Look up the dictionary

a) key The judgment of the

in : designated key There is a return in the dictionary true
not in designated key There is no return in the dictionary true

   #  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38scores={' Internet veteran Xin ':120," Gao Shenghan ":100}print(' Gao Shenghan ' in scores)print(' Gao Shenghan ' not in scores)

 Insert picture description here

b) Deletion of dictionary elements

del socres【 Zhang San 】

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38scores={' Internet veteran Xin ':120," Gao Shenghan ":100}del scores[' Gao Shenghan ']print(scores)

 Insert picture description here
The clearing of dictionary elements :

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38scores={' Internet veteran Xin ':120," Gao Shenghan ":100}scores.clear()print(scores)

c) The addition of dictionary elements

socres[‘itlaoxin’]=90

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38scores={' Internet veteran Xin ':120," Gao Shenghan ":100}scores[' Zhang San ']=120
print(scores)

 Insert picture description here

d) How to get the dictionary view

 Insert picture description here
keys()

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38scores={' Internet veteran Xin ':120," Gao Shenghan ":100}# Get all keys keys=scores.keys()print(keys)print(list(keys))

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38scores={' Internet veteran Xin ':120," Gao Shenghan ":100}# Get all keys values=scores.values()print(list(values))#[120, 100]

items()

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38scores={' Internet veteran Xin ':120," Gao Shenghan ":100}items=scores.items()print(items)print(list(items))

In this way, it is the Yuanzu
 Insert picture description here

3. Element traversal of the dictionary

Traversal of dictionary elements :
for i in scores:
print(item)

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38scores={' Internet veteran Xin ':120," Gao Shenghan ":100}# Traversal of dictionary elements for item in scores:
    print(item)  
    print(scores[item])print(scores.get(item))

4. Dictionary generative

Two lists become a dictionary to use zip()

#  author : Internet veteran Xin #  Development time :2021/4/3/0003 18:38name=[' Internet veteran Xin '," Gao Shenghan ",' Zhang San ']scores=[90,100,120]# Use built-in functions zip()  Become a dictionary c={ name:scores for name,scores in zip(name,scores)}print(c)

 Insert picture description here

5. summary

This article mainly talked about Python The use of the dictionary , Dictionaries are simpler than lists .

The key point is to add, delete, change and search , It needs to be practiced over and over .