- 字典是一系列键—值对。每个键都与一个值相关联,使用键来访问与之相关联的值。查找速度极快。
#与键相关联的值可以是数字、字符串、列表乃至字典。事实上,可将任何Python对象用作字典中的值。
#字典用放在花括号{}中的一系列键—值对表示:
alien_0 = {‘color’: ‘green’, ‘points’: 5}
print(alien_0[‘color’])
print(alien_0[‘points’]) - 添加键—值对
#python以前字典无序,现在有序了
alien_0 = {‘color’: ‘green’, ‘points’: 5}
print(alien_0)
alien_0[‘x_position’] = 0
alien_0[‘y_position’] = 25
print(alien_0) - 修改字典中的值,重新赋值即可
alien_0 = {‘color’: ‘green’}
print(“The alien is “ + alien_0[‘color’] + “.”)
alien_0[‘color’] = ‘yellow’
print(“The alien is now “ + alien_0[‘color’] + “.”) - 删除键—值对,使用del语句进行永久删除
alien_0 = {‘color’: ‘green’, ‘points’: 5}
print(alien_0)
del alien_0[‘points’]
print(alien_0) - 遍历字典
#用于遍历字典的for循环,可声明两个变量,用于存储键—值对中的键和值
#方法items()用于返回一个键—值对列表
#方法keys()用于返回键列表
#方法values()用于返回值列表
user_0 = {
‘username’: ‘efermi’,
‘first’: ‘enrico’,
‘last’: ‘fermi’,
}
for key, value in user_0.items():
print(“\nKey: “ + key)
print(“Value: “ + value)
for key in user_0.keys():
print(“\nKey: “ + key)
for value in user_0.values():
print(“\nvalue: “ + value)
#剔除重复项,可使用集合(set)。集合类似于列表,但每个元素都必须是独一无二的:
favorite_languages = {
‘jen’: ‘python’,
‘sarah’: ‘c’,
‘edward’: ‘ruby’,
‘phil’: ‘python’,
}
print(“The following languages have been mentioned:”)
for language in set(favorite_languages.values()):
print(language.title()) - 嵌套:字典和列表的嵌套层级不宜太多,有更好的处理方法
#字典列表
alien_0 = {‘color’: ‘green’, ‘points’: 5}
alien_1 = {‘color’: ‘yellow’, ‘points’: 10}
alien_2 = {‘color’: ‘red’, ‘points’: 15}
aliens = [alien_0, alien_1, alien_2]
for alien in aliens:
print(alien) - 列表字典
# 存储所点比萨的信息
pizza = {
‘crust’: ‘thick’,
‘toppings’: [‘mushrooms’, ‘extra cheese’],
} - 概述所点的比萨
print(“You ordered a “ + pizza[‘crust’] + “-crust pizza “ +
“with the following toppings:”)
for topping in pizza[‘toppings’]:
print(“\t” + topping) - 每个值都是列表时用两层循环遍历
favorite_languages = {
‘jen’: [‘python’, ‘ruby’],
‘sarah’: [‘c’],
‘edward’: [‘ruby’, ‘go’],
‘phil’: [‘python’, ‘haskell’],
}
for name, languages in favorite_languages.items():
print(“\n” + name.title() + “‘s favorite languages are:”)
for language in languages:
print(“\t” + language.title())
#字典中嵌套字典
users = {
‘aeinstein’: {
‘first’: ‘albert’,
‘last’: ‘einstein’,
‘location’: ‘princeton’,
},
‘mcurie’: {
‘first’: ‘marie’,
‘last’: ‘curie’,
‘location’: ‘paris’,
},
}
for username, user_info in users.items():
print(“\nUsername: “ + username)
full_name = user_info[‘first’] + “ “ + user_info[‘last’]
location = user_info[‘location’]
print(“\tFull name: “ + full_name.title())
print(“\tLocation: “ + location.title())
字典是一系列键—值对。每个键都与一个值相关联,使用键来访问与之相关联的值。查找速度极快。
#与键相关联的值可以是数字、字符串、列表乃至字典。事实上,可将任何Python对象用作字典中的值。
#字典用放在花括号{}中的一系列键—值对表示:
alien_0 = {‘color’: ‘green’, ‘points’: 5}
print(alien_0[‘color’])
print(alien_0[‘points’])
添加键—值对
#python以前字典无序,现在有序了
alien_0 = {‘color’: ‘green’, ‘points’: 5}
print(alien_0)
alien_0[‘x_position’] = 0
alien_0[‘y_position’] = 25
print(alien_0)
修改字典中的值,重新赋值即可
alien_0 = {‘color’: ‘green’}
print(“The alien is “ + alien_0[‘color’] + “.”)
alien_0[‘color’] = ‘yellow’
print(“The alien is now “ + alien_0[‘color’] + “.”)
删除键—值对,使用del语句进行永久删除
alien_0 = {‘color’: ‘green’, ‘points’: 5}
print(alien_0)
del alien_0[‘points’]
print(alien_0)
运行结果:
遍历字典
#用于遍历字典的for循环,可声明两个变量,用于存储键—值对中的键和值
#方法items()用于返回一个键—值对列表
#方法keys()用于返回键列表
#方法values()用于返回值列表
user_0 = {
‘username’: ‘efermi’,
‘first’: ‘enrico’,
‘last’: ‘fermi’,
}
for key, value in user_0.items():
print(“\nKey: “ + key)
print(“Value: “ + value)
for key in user_0.keys():
print(“\nKey: “ + key)
for value in user_0.values():
print(“\nvalue: “ + value)
#剔除重复项,可使用集合(set)。集合类似于列表,但每个元素都必须是独一无二的:
favorite_languages = {
‘jen’: ‘python’,
‘sarah’: ‘c’,
‘edward’: ‘ruby’,
‘phil’: ‘python’,
}
print(“The following languages have been mentioned:”)
for language in set(favorite_languages.values()):
print(language.title())
运行结果:
嵌套:字典和列表的嵌套层级不宜太多,有更好的处理方法
#字典列表
alien_0 = {‘color’: ‘green’, ‘points’: 5}
alien_1 = {‘color’: ‘yellow’, ‘points’: 10}
alien_2 = {‘color’: ‘red’, ‘points’: 15}
aliens = [alien_0, alien_1, alien_2]
for alien in aliens:
print(alien)
列表字典
# 存储所点比萨的信息
pizza = {
‘crust’: ‘thick’,
‘toppings’: [‘mushrooms’, ‘extra cheese’],
}
概述所点的比萨
print(“You ordered a “ + pizza[‘crust’] + “-crust pizza “ +
“with the following toppings:”)
for topping in pizza[‘toppings’]:
print(“\t” + topping)
每个值都是列表时用两层循环遍历
favorite_languages = {
‘jen’: [‘python’, ‘ruby’],
‘sarah’: [‘c’],
‘edward’: [‘ruby’, ‘go’],
‘phil’: [‘python’, ‘haskell’],
}
for name, languages in favorite_languages.items():
print(“\n” + name.title() + “‘s favorite languages are:”)
for language in languages:
print(“\t” + language.title())
#字典中嵌套字典
users = {
‘aeinstein’: {
‘first’: ‘albert’,
‘last’: ‘einstein’,
‘location’: ‘princeton’,
},
‘mcurie’: {
‘first’: ‘marie’,
‘last’: ‘curie’,
‘location’: ‘paris’,
},
}
for username, user_info in users.items():
print(“\nUsername: “ + username)
full_name = user_info[‘first’] + “ “ + user_info[‘last’]
location = user_info[‘location’]
print(“\tFull name: “ + full_name.title())
print(“\tLocation: “ + location.title())
运行结果:
