6.4.1 字典列表
相当于结构体数组的概念。
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)
6.4.2 在字典中存储列表
即一个key对应多个value的情形。
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())
6.4.3 在字典中存储字典
users = {
'aeinstein': {
'first': 'albert',
'last': 'curcle',
'location': 'paris',
},
'mcurie': {
'fisrt': '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: " + ful_name.title())
print("\tLocation: " + location.title())