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

Five basic operation methods for finding data in Python dictionary

編輯:Python

Catalog

Preface

One 、key Value search

Two 、 Function lookup

2.1 get()

2.2 keys()

2.3 values()

2.4 items()

attach : Common ways of Dictionary

summary

Preface

The last article wrote about the addition, deletion and modification of dictionary operation methods , This article mainly explains how to find dictionary data . There are two ways to write search data , One can be key Value search , The other is to search the data according to the writing method of the function .

One 、key Value search

If the current search key There is , Then return the corresponding value , Otherwise, an error will be reported .

Code example :

dict1 = {'name': 'Rose', 'age': 30, 'sex': ' Woman '}# key If it exists, the corresponding value is returned print(dict1['name']) # Rose# key If it doesn't exist, it's wrong print(dict1['id']) # Report errors Two 、 Function lookup 2.1 get()

grammar :

Dictionary sequence .get(key, The default value is )

Be careful :

If the current search key If not, the second parameter is returned ( The default value is ), If you omit the second parameter , Then return to None.

Quick experience :

dict1 = {'name': 'Rose', 'age': 30, 'sex': ' Woman '}# key There is print(dict1.get('name')) # Roseprint(dict1.get('name', 'python')) # Rose# key non-existent , The second parameter exists , Return the second parameter print(dict1.get('id', 1010)) # 1010# key non-existent , The second parameter does not exist , return Noneprint(dict1.get('id')) # None2.2 keys()

grammar :

Dictionary sequence .keys()

effect :

Look up all the... In the dictionary key, Returns the iteratable object ( A collapsible iteration object is one that can use for Traversal object )

Quick experience :

dict1 = {'name': 'Rose', 'age': 30, 'sex': ' Woman '}print(dict1.keys())# result dict_keys(['name', 'age', 'sex'])# The result is an iteratable object , use for Traversal object 2.3 values()

grammar :

Dictionary sequence .values()

effect :

Look up all the... In the dictionary value, Returns the iteratable object ( A collapsible iteration object is one that can use for Traversal object )

Quick experience :

dict1 = {'name': 'Rose', 'age': 30, 'sex': ' Woman '}print(dict1.values())# result dict_values(['Rose', 30, ' Woman '])# The result is an iteratable object , use for Traversal object 2.4 items()

grammar :

Dictionary sequence .get(key, The default value is )

effect :

Look up all the key value pairs in the dictionary , Returns the iteratable object , The data in it is tuples , Tuple data 1 It's a dictionary key, Tuple data 2 It's a dictionary key Corresponding value .

Quick experience :

dict1 = {'name': 'Rose', 'age': 30, 'sex': ' Woman '}print(dict1.items())# result dict_items([('name', 'Rose'), ('age', 30), ('sex', ' Woman ')])dict1 = {'name': 'Rose', 'age': 30, 'sex': ' Woman '}print(dict1.items())# result dict_items([('name', 'Rose'), ('age', 30), ('sex', ' Woman ')])# The result is an iteratable object attach : Common ways of Dictionary """ Dictionary definition 、 Add and modify """dictA = {} # An empty dictionary , There's no data dictA['name'] = 'lihua' # Add a key value pair to the dictionary ,name yes key,lihua yes valueprint(dictA) # Output complete dictionary dictB = {'name': 'xiaogang', 'age': 18, 'job': [student, cook]} # Add key value pairs when creating a dictionary print(len(dictB)) # Dictionaries can also be used through len Function to get the length of the key value pair print(dictB['name']) # Because in the dictionary key Have uniqueness , So you can look up key To get the corresponding valuedictB['name'] = 'xiaowang' # modify key The corresponding value""" The key in the dictionary 、 value 、 Acquisition of key value pairs """dictB = {'name': 'lh', 'age': '18', 'job': 'student'}print(dictB.keys()) # Get all the keys in the dictionary print(type(dictB.keys)) # The data type is dict_keysprint(dictB.values()) # Get all the values in the dictionary print(type(dictB.values())) # The data type is dict_valuesprint(dictB.items()) # Get all the key value pairs in the dictionary print(type(dictB.items())) # The data type is dict_values""" Traversal operation of dictionary """dictB = {'name': 'lh', 'age': '18', 'job': 'student'}for item in dictB.items(): print(item) # Dictionaries are traversable passfor key, value in dictB.items(): print(key + '==' + value) # You can use two temporary variables to receive the values of key value pairs key and value""" Dictionary update 、 modify """dictB = {'name': 'lh', 'age': '18', 'job': 'student'}dictB.update({'name': 'lki'}) # utilize update Function can modify key value pairs dictB.update({'height': '159'}) # update Function can also add key value pairs , It has the feature of updating dictionary """ Deletion of key value pairs """dictB = {'name': 'lh', 'age': '18', 'job': 'student'}del dictB['name'] # By designation key You can delete key value pairs dictB.pop('age') # By designation key To delete dictB.clear() # Empty dictionary del dictB # Delete Dictionary summary

This is about Python A dictionary for finding data 5 This is the end of the article on basic operation methods , More about Python Please search the previous articles of the software development network or continue to browse the relevant articles below. I hope you will support the software development network in the future !



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