第6章 字典
pets = [
{
'name': 'miao',
'type': 'cat',
'owner': 'Sam',
},
{
'name': 'wang',
'type': 'dog',
'owner': 'Bob',
}
]
for pet in pets :
print(pet['name'] + ':')
print('\ttype: ' + pet['type'])
print('\towner: ' + pet['owner'])
favorite_places = {
'Alice': [
'cinema', 'park', 'game center'
],
'Bob': [
'stadium'
],
'Cindy': [
'gym', 'concert hall'
],
}
for name, places in favorite_places.items() :
print(name + ':')
for place in places :
print('\t' + place)
cities = {
'Shanghai': {
'country': 'China',
'pop': 1000000,
'fact': "There's one Disneyland there",
},
'Beijing': {
'country': 'China',
'pop': 2000000,
'fact': 'The capital of China',
},
}
for city, infos in cities.items() :
print(city + ':')
print('\t' + 'country: ' + infos['country'])
print('\t' + 'population: ' + str(infos['pop']))
print('\t' + 'fact: ' + infos['fact'])