#字典,访问字典中的值
alien_0 = {'color':'green','points':5}
print(alien_0['color'])
print(alien_0['points'])
green
5
#将值存储在变量中new_points中
new_points = alien_0['points']
print("You just earned " + str(new_points) + " points!")
You just earned 5 points!
#添加键-值对;指定字典名,用方括号括起的键和相关联的值
alien_0 = {'color':'green','points':5}
print(alien_0)
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}
#先创建一个空字典
alien_0 = {}
alien_0['color'] = 'green'
alien_0['points'] = 5
print(alien_0)
{'color': 'green', 'points': 5}
#修改字典中的值
alien_0 = {'color':'green'}
print("The alien is " + alien_0['color'] + ".")
The alien is green.
alien_0 = {'color':'yellow'}
print("The alien is now " + alien_0['color'] + ".")
The alien is now yellow.
#创建一个字典
alien_0 = {'x_position':0, 'y_position':25, 'speed':'medium'}
print("Original x-position: " + str(alien_0['x_position']))
Original x-position: 0
if alien_0['speed'] == 'slow':
x_increment = 1
elif alien_0['speed'] == 'medium':
x_increment = 2
else:
x_increment = 3
alien_0['x_position'] = alien_0['x_position'] + x_increment#通过修改外星人字典中的值,可改变外星人的行为。
print(str(alien_0['x_position']))
2
2
#删除键值对
alien_0 = {'color':'green','points':5}
print(alien_0)
del alien_0['points']
print(alien_0)
{'color': 'green', 'points': 5}
{'color': 'green'}
#由类似对象组成的字典,注意这个地方的逗号!
fav_flo = {
'baba':'hehua',
'mama':'meihua',
'jingjing':'xiangrikui'
}
print("Jingjing's favorite flower is " + fav_flo['jingjing'].title() + ".")
Jingjing's favorite flower is Xiangrikui.
#遍历字典
fav_flo = {
'baba':'hehua',
'mama':'meihua',
'jingjing':'xiangrikui'
}
for key, value in fav_flo.items():
print("
Key:" + key)
print("Value:" + value)
Key:baba
Value:hehua
Key:mama
Value:meihua
Key:jingjing
Value:xiangrikui
#遍历字典中所有键值对
for key, value in fav_flo.items():
print("
" + key.title() + "'s favorite flower is " + value.title() + "!")
Baba's favorite flower is Hehua!
Mama's favorite flower is Meihua!
Jingjing's favorite flower is Xiangrikui!
#遍历字典中所有键
for key in fav_flo.keys():
print(key)
baba
mama
jingjing
ai = ['baba','mama']
for key in fav_flo.keys():
print(key.title())
Baba
Mama
Jingjing
favorite_languages = {
'1':'python',
'2':'c',
'3':'ruby',
'4':'python',
}
friends = ['1', '2']
for name in favorite_languages.keys():
print(name.title())
#bug
for name in friends:
print(name.title())
1
2
3
4
1
2
for name in friends:
print("Hi" + name.title() + ", I see your favorite languages is " + favorite_languages[name].title() + "!")
Hi1, I see your favorite languages is Python!
Hi2, I see your favorite languages is C!
favorite_languages['2'].title()
'C'
if '5' not in favorite_languages.keys():
print("ahhaha")
child_age = {
'yue':'12',
'zai':'10',
'lu':'19'
}
for name, age in child_age.items():
print(name.title() + age.title())
Yue12
Zai10
Lu19
didi = ['zai', 'lu']
for name in didi:
print("
" + name.title() + child_age[name].title())
#按顺序遍历字典中的所有键
favorite_languages = {
'1':'python',
'2':'c',
'3':'ruby',
'4':'python',
}
for name in sorted(favorite_languages.keys()):
print(name.title() + "hihi")
#遍历字典中的所有值
favorite_languages = {
'1':'python',
'2':'c',
'3':'ruby',
'4':'python',
}
for language in favorite_languages.values():
print(language.title())
#遍历唯一的值
for language in set(favorite_languages.values()):
print(language.title())
C
Python
Ruby
#嵌套,在列表中嵌套字典,或者在字典中嵌套表
#字典列表
alien_0 = {'color':'green', 'points':5}
alien_1 = {'color':'red', 'points':4}
alien_2 = {'color':'blue', 'points':3}
aliens = [alien_0, alien_1, alien_2]
for alien in aliens:
print(alien)
{'color': 'green', 'points': 5}
{'color': 'red', 'points': 4}
{'color': 'blue', 'points': 3}
aliens = []
for alien_number in range(30):
new_alien ={'color':'green', 'points':5, 'speed':'slow'}
aliens.append(new_alien)
for alien in aliens[:5]:
print(alien)
print("...")
print("Total number of aliens:" + str(len(aliens)))
{'color': 'green', 'points': 5}
{'color': 'red', 'points': 4}
{'color': 'blue', 'points': 3}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
...
Total number of aliens:33
witchs = []
for witch_number in range(30):
new_witch = {'color':'green', 'points':5, 'speed':'slow'}
witchs.append(new_witch)
for witch in witchs[:5]:
print(witch)
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
for witch in witchs[0:3]:
if witch['color'] == 'green':
witch['color'] = 'yellow'
witch['speed'] = 'medium'
witch['points'] = 10
for witch in witchs[:5]:
print(witch)
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
#在字典中存储列表
pizza = {
'crust': 'thick',
'toppings': ['mushroom', 'extra cheese'],
}
print("You order a " + pizza['crust'] + "-crust pizza" + "with the following toppings")
for topping in pizza['toppings']:
print(" " + topping)
You order a thick-crust pizzawith the following toppings
mushroom
extra cheese
aaa_111 = {
'a':['1', '2'],
'b':['3', '4'],
'c':['5', '4'],
}
for key, values in aaa_111.items():
print("
" + key.title() + " hahah")
for value in values:
print(" " + value.title())
A hahah
1
2
B hahah
3
4
C hahah
5
4
#在字典中存储字典
users = {
'aeinstein':{
'aa':'aaa',
'bb':'bbb',
'cc':'ccc',
},
'mcurie':{
'aa':'ddd',
'bb':'fff',
'cc':'ggg',
},
}
for username, user_info in users.items():
print("
Username: " + username)
full_name = user_info['aa'] + " " + user_info['bb']
location = user_info['cc']
print(full_name)
print(location)
Username: aeinstein
aaa bbb
ccc
Username: mcurie
ddd fff
ggg