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
==============================