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

xiaoxiao2021-02-28  50

第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'])
转载请注明原文地址: https://www.6miu.com/read-2613356.html

最新回复(0)