【python基礎】字典
6字典
6.1一個簡單的字典
alien_0 = {'color':'blue','points':5}
print(alien_0)
print(alien_0['color'])
print(alien_0['points'])
{‘color’: ‘blue’, ‘points’: 5}
blue
5
6.2使用字典
6.2.1訪問字典中元素
alien_0 = {'color':'blue','points':5}
new_points = alien_0['points']
print("You have earned "+str (new_points)+" points")
You have earned 5 points
6.2.2給字典新增新的鍵值對
# 給外星人新增x,y座標
alien_0 = {'color':'blue','points':5}
alien_0['x_points']=0
alien_0['y_points']=0.8
print(alien_0)
{‘color’: ‘blue’, ‘points’: 5, ‘x_points’: 0, ‘y_points’: 0.8}
6.2.3先建立一個空字典
alien_0 = {}
#分行新增各個鍵值對
alien_0['color'] = 'red'
alien_0['points'] = 5
print(alien_0)
{‘color’: ‘red’, ‘points’: 5}
6.2.4修改字典中的值
alien_0 = {'color':'red'}
print("The alien color is "+alien_0['color'])
#修改值
alien_0['color'] = 'yellow'
print("The alien color now is "+alien_0['color'])
The alien color is red
The alien color now is yellow
6.2.5刪除字典中的鍵值對
alien_0 = {'color':'blue','points':5}
del alien_0['color']
print(alien_0)
{‘points’: 5}
6.3遍歷字典
6.3.1遍歷所有鍵值對
user = {
'uesrname':'admin',
'password':'123456',
'age':'20',
'major':'Computer Science'
}
for key,value in user.items():
print("\nKey:",key)
print("Value:",value)
Key: uesrname
Value: admin
Key: password
Value: 123456
Key: age
Value: 20
Key: major
Value: Computer Science
6.3.2遍歷所有鍵
user = {
'uesrname':'admin',
'password':'123456',
'age':'20',
'major':'Computer Science'
}
for key in user.keys():
print(key)
uesrname
password
age
major
6.3.3按順序遍歷所有鍵
user = {
'uesrname':'admin',
'password':'123456',
'age':'20',
'major':'Computer Science'
}
for key in sorted(user.keys()):
print(key)
age
major
password
uesrname
6.3.4遍歷所有值
user = {
'uesrname':'admin',
'password':'123456',
'age':'20',
'major':'Computer Science'
}
for value in user.values():
print(value)
admin
123456
20
Computer Science
6.4巢狀
6.4.1字典列表
alien_0 = {'color':'red','points':5}
alien_1 = {'color':'blue','points':10}
alien_2 = {'color':'yellow','points':15}
aliens = [alien_0,alien_1,alien_2]
for alien in aliens:
print(aliens)
[{‘color’: ‘red’, ‘points’: 5}, {‘color’: ‘blue’, ‘points’: 10}, {‘color’: ‘yellow’, ‘points’: 15}]
[{‘color’: ‘red’, ‘points’: 5}, {‘color’: ‘blue’, ‘points’: 10}, {‘color’: ‘yellow’, ‘points’: 15}]
[{‘color’: ‘red’, ‘points’: 5}, {‘color’: ‘blue’, ‘points’: 10}, {‘color’: ‘yellow’, ‘points’: 15}]
6.4.2在字典中儲存列表
pizza = {
'crust':'thick',
'toppings':['mushrooms','extra cheese']
}
print(pizza['toppings'])
[‘mushrooms’, ‘extra cheese’]
6.4.3在字典中儲存字典
users = {
'tom':{
'age': 21,
'password':'123456',
'location':'zhengzhou'
},
'jerry':{
'age': 22,
'password':'123456',
'location':'beijing'
},
}
for userName,userInfo in users.items():
print("\nUsername:"+userName)
user_age = str(userInfo['age'])
user_password = userInfo['password']
user_location = userInfo['location']
print("\nAge:"+user_age)
print("\nPassword:"+user_password)
print("\nLocation:"+user_location)
print("\n==============================")
Username:tom
Age:21
Password:123456
Location:zhengzhou
==============================
Username:jerry
Age:22
Password:123456
Location:beijing
==============================