Dictionaries are also one of the most commonly used data types in the development process. If you need to store information about a class student, each student has a name, gender, age, address, and so on, and if you store it in a list, you need a lot of lists. In this time, we need to use a dictionary to define. Dictionary, Dict,dictionary.
names = [' Amy ', ' Lily ', ' Ben ']infos = [[], ' female ', ' Beijing '],[17, ' female ', ' Tianjin '],[21, ' Male ', ' Shanghai ']
1. Dictionary definition
A dictionary is a Key-value data type, using the {} definition, where each value is separated by "," separated by ":" for key and value.
For example, the information of a class student can be written into a dictionary, the student's number is key, and the student's information is value.
1 infos = {2 ' Amy ': [Female ', ' Beijing '],3 ' Lily ': [+, ' female ', ' Tianjin '],4 ' Ben ': [+] ' male ', ' Shanghai ']5}6 print ( infos[' Amy ')
Features of the dictionary:
(1) Easy access to data
(2) Fast search speed
Why does the dictionary look faster?
If you use the list to store class student information, you need to query a student's information, you need to find his location from names, and then find his information from Infos, if the longer the list, the query will be slower.
In a dictionary, you only need a table with a name and a message, so you can quickly find the corresponding information by name, no matter how big the table is, and how fast it will look.
This is the same as checking the dictionary. If you look at a word, from the first page to the last page, then the dictionary is about large, the slower the query, this method is to find the element in the list method. Another way is to find the corresponding page number of the word in the index of the dictionary, and then directly to the page to find the word, no matter which word is quickly, not as the dictionary becomes larger and slower, this is the way the dictionary is implemented.
Features of the dictionary:
(1) The dictionary is non-continuous because it has no subscript and is indexed with key.
(2) The key of the dictionary must be unique, the key cannot be duplicated and is inherently heavy.
2. Dictionary operation
(1) Increase: assignment, SetDefault ()
1 infos = {2 ' Amy ': [+, ' female ', ' Beijing '], 3 ' Lily ': [+, ' female ', ' Tianjin '], 4 ' Ben ': [, ' Male ', ' Shanghai '] 5} 6 infos[' Kevin '] = [male ', ' Beijing '] #通过赋值增加 7 infos.setdefault (' Heby ', [9, ' Female ', ' Shanghai ']) #通过setdefault增加 8 print (infos) infos[ ' Kevin '] = [male ', ' XI ' an '] #如果key存在, will modify the original key corresponding to the Value10 infos.setdefault (' Amy ', [female ', ' XI ' an ']) #如果key存在, VALUE11 print (infos) corresponding to the original key will not be modified
(2) Delete: Pop (), Popitem (), Del, Clear ()
1 infos = {2 ' Amy ': [Female ', ' Beijing '], 3 ' Lily ': [+, ' female ', ' Tianjin '], 4 ' Ben ': [+, ' Male ', ' Shanghai '] 5} 6 Infos.pop (' Lily ') #指定key来删除 7 infos.popitem () #随机删除一个key, not commonly used 8 del infos[' Ben '] #指定key来删除 9 infos.clear ( ) #清空字典10 Print (infos)
(3) Modify: Assign Value
1 infos = {2 ' Amy ': [Female ', ' Beijing '],3 ' Lily ': [+, ' female ', ' Tianjin '],4 ' Ben ': [+] ' male ', ' Shanghai ']5}6 infos[' Ben '] = [male ', ' XI ' an ']7 print (infos)
(4) Find: Get (), direct value
1 infos = {2 ' Amy ': [Female ', ' Beijing '], 3 ' Lily ': [+, ' female ', ' Tianjin '], 4 ' Ben ': [+, ' Male ', ' Shanghai '] 5} 6 ' Amy ' in I NFOs #判断Amy是否在字典中, returns TRUE or False 7 print (Infos.get (' Lily ')) 8 print (Infos.get (' Lucy ')) #如果取不到这个key, returns none 9 print ( Infos.get (' Lucy ') #如果取不到这个key, the default is 11010 print (infos[' Ben ') "Print (infos[' Kevin ')") #如果key不存在会报错
3. Dictionary built-in methods
Keys (), values (), items (), get (), SetDefault (), update ()
1 infos = {2 ' Amy ': [+, ' female ', ' Beijing '], 3 ' Lily ': [+, ' female ', ' Tianjin '], 4 ' Ben ': [+, ' Male ', ' Shanghai '] 5} 6 Infos2 = { ' Kevin ': [Male ', ' Shanghai ']} 7 print (Infos.keys ()) #获取到字典所有的keys 8 print (Infos.values ()) #获取到字典所有的values 9 print ( Infos.items ()) # Gets the dictionary all K-V10 print (Infos.get (' Amy's)) Print (Infos.setdefault (' Ben ', [+] ' male ', ' Shanghai ')) 12 Infos.update (INFOS2) #更新字典值, if the key exists, it will be updated, if it does not exist, add the print (infos)
4. Multiple dictionaries
1 infos = {2 ' Amy ': {3 ' age ': $4 ' money ': 200000, 5 ' clothes ': ' 100 sets ', 6 ' hzp ': ' N More ', 7 ' shoes ' : [' Nike ', ' Addis ', ' LV ', ' Chanle '] 8 }, 9 ' Lily ': {ten ' vaults ': ' 2000w ', ' House ': [' Three rings set ', ' 4 rings 2 sets '],12 ' Cars ': {' Japan ': [' Prado ', ' Rand Cool Road ze '],14 ' USA ': [' Lincoln ', ' Cadillac ', ' Ford '],15 ' China ': [' wuling ', ' qq ', ' Red Flag ']16 } }18}19 infos[' Lily ' [' Cars '] [' USA '].append (' Wrangler ') infos[' Amy ' [' Shoes '].append (' converse ') ' infos[' Amy ' [' Money '] = infos[' Amy ' [' money '] +20022 print (infos)
5, the Dictionary of the Cycle
To loop a dictionary directly, then the dictionary key is recycled.
1 infos = {2 ' Amy ': [+, ' female ', ' Beijing '], 3 ' Lily ': [+, ' female ', ' Tianjin '], 4 ' Ben ': [+, ' Male ', ' Shanghai '] 5} 6 for key in Infos:7 Print (Key, '---', Infos[key]) #打印key和value的值, recommended this way, fast 8 9 for k,v in Infos.items (): print (k, ' ===> ', v) #打印key和value的值, this method is not recommended, because the dictionary will be converted into a list, the efficiency is not high
Python3 Study Notes-dictionary (dict)