程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

4.11 Python dictionary

編輯:Python

Python Data type No 1 Kind of : character string (str), What is enclosed in quotation marks .
Python Data type No 2 Kind of : Integers (int).
Python Data type No 3 Kind of : Floating point numbers (float), A number with a decimal point .
Python Data type No 4 Kind of : list (list), In square brackets [ ] Express .
Python Data type No 5 Kind of : Tuples (tuple), Use parentheses ( ) Express .
Python Data type No 6 Kind of : Dictionaries (dict), Use braces { } Express .

4.11.1 The concept of a dictionary

list 、 Tuples store ordered data , Orderly means by 0、1、2、3 In the order of .
Dictionaries store data with mapping relationships , Mapping can be understood as a correspondence .
list 、 The data stored in tuples is called elements .
The data stored in the dictionary is called a key value pair , Is an unordered sequence .
Dictionary use { } Express , Use English commas between key value pairs , Separate , Use English colon between key and value : Separate .
Dictionaries are variable data types , You can add, delete or change key value pairs .
Be careful :dict As Python Keywords and built-in functions , Variable names are not recommended to be named dict.
dictionary[ˈdɪkʃənri]: Dictionaries
The dictionary syntax is shown in the following figure :

4.11.2 New dictionary

# Create a new empty dictionary :
empty_dict = {
 }

Create a new dictionary to store personal information

# Create a new dictionary 
info_dict = {
 ' full name ':' Bai Jingting ',' Date of birth ':'1993 year 10 month 15 Japan '}
# View dictionary 
info_dict

【 Terminal output 】

{' full name ': ' Bai Jingting ', ' Date of birth ': '1993 year 10 month 15 Japan '}

There are two key value pairs in the above dictionary ,‘ full name ’: ' Bai Jingting ’ Is the first ,‘ Date of birth ’: '1993 year 10 month 15 Japan ’ It's the second one. .
' full name ’ It's a key ,' Bai Jingting ’ Is the value of the key .

4.11.3 Add key value to

grammar : Dictionaries [ key ]=[ value ]

# Create a new dictionary 
info_dict = {
 ' full name ':' Bai Jingting ',' Date of birth ':'1993 year 10 month 15 Japan '}
# Add a key value pair : height 188.8
info_dict[' height '] = 188.8
# View dictionary 
info_dict

【 Terminal output 】

{' full name ': ' Bai Jingting ', ' Date of birth ': '1993 year 10 month 15 Japan ', ' height ': 188.8}

4.11.4 Delete key value pair

grammar :del Dictionaries [ key ]
del After space .
delete [dɪˈliːt]: Delete .

# Create a new dictionary 
info_dict = {
 ' full name ':' Bai Jingting ',' Date of birth ':'1993 year 10 month 15 Japan '}
# Delete a key value pair : Date of birth 
del info_dict[' Date of birth ']
# View dictionary 
info_dict

【 Terminal output 】

{' full name ': ' Bai Jingting '}

4.11.5 Value modification

grammar : Dictionaries [ key ]=[ The new value ]

# Create a new dictionary 
info_dict = {
' full name ': ' Bai Jingting ', ' Date of birth ': '1993 year 10 month 15 Japan ', ' height ': 188.8}
# Change the height to 180
info_dict[' height '] = 180
# View dictionary 
info_dict

【 Terminal output 】

{' full name ': ' Bai Jingting ', ' Date of birth ': '1993 year 10 month 15 Japan ', ' height ': 180}

4.11.6 Dictionary value extraction

Method 1 : Dictionaries [ key ]
Method 2 : Dictionaries .get( key )
Method 3 : Dictionaries [ key 1][ key 2]

Method 1 : Dictionaries [ key ]

# Create a new dictionary 
info_dict = {

' Basic information ':(' Bai Jingting ',' male ','1993 year '),
' Personal hobbies ':[' Bodybuilding ',' swimming '],
' Contact information ':{
' Wechat number ':'bjt77',' Phone number ':18087578757}
}
# Check the dictionary for personal hobbies 
info_dict[' Personal hobbies ']

【 Terminal output 】

[' Bodybuilding ', ' swimming ']

use info_dict[‘ Personal hobbies ’] You can view the dictionary key personal hobby value Fitness 、 swimming .

Method 2 : Dictionaries .get( key )

# Create a new dictionary 
info_dict = {

' Basic information ':(' Bai Jingting ',' male ','1993 year '),
' Personal hobbies ':[' Bodybuilding ',' swimming '],
' Contact information ':{
' Wechat number ':'bjt77',' Phone number ':18087578757}
}
# Check the dictionary for personal hobbies 
info_dict.get(' Personal hobbies ')

【 Terminal output 】

[' Bodybuilding ', ' swimming ']

Method 3 : Dictionaries [ key 1][ key 2]

# Create a new dictionary 
info_dict = {

' Basic information ':(' Bai Jingting ',' male ','1993 year '),
' Personal hobbies ':[' Bodybuilding ',' swimming '],
' Contact information ':{
' Wechat number ':'bjt77',' Phone number ':18087578757}
}
# Check wechat number 
info_dict[' Contact information '][' Wechat number ']

【 Terminal output 】

'bjt77'

Dictionary values are also dictionaries , Using a dictionary [ key 1][ key 2] The way to take value .

4.11.7 Data types of keys and values

# Create a new dictionary 
info_dict = {

' Basic information ':(' Bai Jingting ',' male ','1993 year '),
' Personal hobbies ':[' Bodybuilding ',' swimming '],
' Contact information ':{
' Wechat number ':'bjt77',' Phone number ':18087578757}
}
# View dictionary 
info_dict

【 Terminal output 】

{' Basic information ': (' Bai Jingting ', ' male ', '1993 year '),
' Personal hobbies ': [' Bodybuilding ', ' swimming '],
' Contact information ': {' Wechat number ': 'bjt77', ' Phone number ': 18087578757}}

In the above Dictionary 3 Key value pairs :
The first key Basic information The value of is tuple , Parentheses denote tuples :
The second key Personal hobbies The value of is list , Brackets indicate a list ;
The third key Contact information The value of is dictionary , Curly braces mean dictionaries .

The keys in the dictionary are immutable data types , Like strings ;
The values in the dictionary can be of any data type .
You can write your own code test .

4.11.7 summary

Practice after class

Create a dictionary that contains the following :
A grade has 3 A class , Class one 、 Class two 、 Class three ;
Class one 50 people , average 70 branch , The highest 88, Lowest score 66;
Class two 60 people , average 80 branch , The highest 99, Lowest score 77;
Class three 70 people , average 90 branch , The highest 99, Lowest score 88.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved