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

Python Basics - Dictionary

編輯:Python

1. Dictionaries :

stay Python in , Dictionaries Is a series of keys — It's worth it . Each key is associated with a value , You can use the key to access the value associated with it . The value associated with the key can be a number 、 character string 、 Lists and even dictionaries . in fact , Any Python Object is used as a value in the dictionary . stay Python in , The dictionary is used in Curly braces {} A series of keys in — Value pairs indicate

key — value Yes, there are two associated values . When you specify the key ,Python Will return the value associated with . Between key and value The colon Separate , And the key — Value pairs are separated by commas . In the dictionary , How many keys do you want to store — Value pairs are OK . The simplest dictionary has only one key - It's worth it

#1.1 Access the values in the dictionary
pets = {'name': 'xixi'}
print(pets['name'])
# Output xixi
#1.2 Add key - It's worth it
pets = {'name': 'xixi'}
pets['color'] = green
#1.3 Create an empty dictionary
# You can first define an empty dictionary with a pair of empty curly braces , Then add each key in a line — It's worth it
pets = {}
pets['color'] = green
pets['name'] = xixi
#1.4 Change the value in the dictionary
pets = {'name': 'xixi'}
print("The pet name is " + pets['name'] + ".")
pets['color'] = green
print("The pet color is " + pets['color'] + ".")
#1.5 Delete key - It's worth it
# You can use del Statement will correspond to the key — Delete value pairs completely
pets = {'name': 'xixi'}
del pets['name']

2. A simple dictionary :

pets = {'color': 'green', 'name':kiki}
print(pets['color'])
print(pets['name'])

The printed result is :green xixi

3. Ergodic dictionary :

3.1 Go through all the keys - It's worth it key value

pets = {'name': 'xixi','age':'3','color':'white'} 

for key, value in pets.items():

        print("\nKey: " + key)

        print("Value: " + value)    

To write a for traversing the dictionary for loop , You can declare two variables key and value, Used to store keys — The key and value in a value pair . For these two variables , You can use any name .

3.2 Traverse all keys and values , When the values in the dictionary are not needed , Use Method keys() , When keys are not needed , Usage method values()

pets = {'name': 'xixi','age':'3','color':'white'}

for name in pets.keys(): Or you could write it as for name in pets: The output will not change .

for name in favorite_languages.values(): // Traverse all values , This method extracts all the values in the dictionary , There is no consideration of whether to repeat , If change to for name in set(favorite_languages.values()): , Allowing Python Find the unique elements in the list , And use these elements to create a collection

3.3 Traverse all the keys in the dictionary in order

You can use functions sorted() 

pets = {'name': 'xixi','age':'3','color':'white'}
for name in sorted(pets.keys()):
print(name.title() )

4. nesting :

You need to store a series of dictionaries in a list , Or store the list as a value in the dictionary , This is called nesting . You can nest dictionaries in a list 、 Nesting lists in dictionaries and even dictionaries in dictionaries .

4.1 Dictionary list

pets0 = {'name': 'xixi','age':'3','color':'white'}
pets1 = {'name': 'kk','age':'1','color':'white'}
pets = [pets0,pets1]

4.2 Storing lists in a dictionary :

4.3 Store a dictionary in a dictionary

The learning content of this article comes from 《Python Programming : From introduction to practice 》


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