python课程作业——第6章 字典

第6章 字典

这里写图片描述

# 6-8
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'])

# 6-9
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)

# 6-11
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'])

猜你喜欢

转载自blog.csdn.net/yeziqing10/article/details/80628755