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

Access to the dictionary of Python Foundation

編輯:Python

About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .


One 、 background

In the actual development process , We will encounter situations where we need to correlate relevant data , for example , Deal with student id 、 full name 、 Age 、 Grades and other information . in addition , There will also be situations where different objects that can be identified need to be considered as a whole .Python Two data structures, dictionary and set, are provided to solve the above problems . Here is an introduction python Access to dictionaries .


Two 、 Access to dictionaries

1. According to the key access value

Each element in the dictionary represents a mapping relationship , Will provide “ key ” As a subscript, you can access the corresponding “ value ”, If this does not exist in the dictionary “ key ” An exception will be thrown . The syntax is as follows :

Dictionary variable name [ key ]

for example :

stu_info = {'num': '20180105', 'name': 'Yinbing', 'sex': 'male'} # Create a dictionary
print(stu_info['num']) # according to num Access student ID
print(stu_info['age']) # There is no exception thrown for the specified key

give the result as follows .

2. Use get() Method access value

When accessing the dictionary , If you are not sure if there is a key in the dictionary , It can be done by get() Method to get , If the key exists , The corresponding value is returned , If it does not exist , Returns the default value . The syntax is as follows :

  • dict Is the name of the visited dictionary
  • key Is the key to find
  • default Define default values , If the value of the specified key does not exist , Returns the default value , When default It's empty time , return None

dict.get(key[,default=None]) for example :

stu_info = {'num': '20180105', 'name': 'Yinbing', 'sex': 'male'} # Create a dictionary
print(stu_info.get('name')) # get() Get student name
print(stu_info.get('age')) # get() Get student age , The return value is None
print(stu_info.get('age')) # Output return value None
print(stu_info.get('age', 18)) # Set the return default value to 18

give the result as follows .


3、 ... and 、 Reference resources

1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial


Four 、 summary

The above is about python Access to dictionaries , You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .


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