# 6-1 friend1 = { 'first_name' : 'Donald', 'last_name' : 'Trump', 'age' : '71', 'city' : 'New York', } for key in friend1: print(key + ' is '+friend1[key]) # 6-5 rivers = { 'changjiang river' : 'china', 'mississippi river' : 'america', 'nile' : 'egypt' } for key in rivers.keys(): print('The ' + key.title() + ' runs through ' + rivers[key].title() + '.') # 6-7 friend2 = { 'first_name' : 'Barack', 'last_name' : 'Obama', 'age' : '56', 'city' : 'Hawaii', } friend3 = { 'first_name' : 'Cheng', 'last_name' : 'Long', 'age' : '63', 'city' : 'Hong Kong' } people = [friend1 , friend2 , friend3] for fri in people: for key in fri.keys(): print(key.title() + ':' + fri[key] ) print('') # 6-9 favorite_places = { 'Trump' : ['Shanghai' , 'Beijing'], 'Obama' : ['Guangzhou', 'Hangzhou'], 'Mike' : ['Xiamen','Beijing'] } for friend in favorite_places: print('My friend ' + friend + "'favorite places are ", end='') for place in favorite_places[friend]: print(place , end = ' ') print() # 6-10 cities ={ 'Guangzhou' : { 'country' : 'China', 'population(thousand)' : 14498, 'fact' : 'The city is in the south of China.' , }, 'London' : { 'country' : 'England', 'population(thousand)' : 8280, 'fact' : 'It is the capital of England.' , }, 'New York' : { 'country' : 'America', 'population(thousand)' : '8510', 'fact' : "It's an international city." , }, } for key_city in cities: print(key_city + ':') for inf in cities[key_city].keys(): print('\t' + inf + ':' + str(cities[key_city][inf]))
3.21python作业
最新推荐文章于 2024-02-20 13:23:44 发布
这篇博客展示了Python中对字典的各种遍历操作,包括打印字典键值、组合多个字典为列表、展示朋友喜欢的城市以及详细城市信息。涉及到的关键字有字典遍历、键值对、城市信息等。
1万+

被折叠的 条评论
为什么被折叠?



