列表

详情


NP70. 首都

描述

创建一个依次包含键-值对'Beijing': {Capital: 'China'}、'Moscow': {Capital: 'Russia'}'Paris': {Capital: 'France'}的字典cities_dict,
请使用for循环遍历"已使用sorted()函数按升序进行临时排的包含字典cities_dict的所有键的列表"
对于每一个遍历到的城市名,使用print()语句一行输出类似字符串'Beijing is the capital of China!'的语句。

输入描述

输出描述

按题目描述进行输出即可。
Beijing is the capital of China!
Moscow is the capital of Russia!
Paris is the capital of France!

原站题解

Python 解法, 执行用时: 10ms, 内存消耗: 2832KB, 提交时间: 2022-06-27

cities_dict = {'Beijing':{'Capital': 'China'},'Moscow':{'Capital': 'Russia'},'Paris':{'Capital': 'France'}}
keys=sorted(cities_dict)
for key in keys:
    aaa = cities_dict[key]
    print '{} is the capital of {}!'.format(key,aaa['Capital'])

Python 解法, 执行用时: 10ms, 内存消耗: 2836KB, 提交时间: 2022-06-10

a = {
    "Beijing": {"Capital": "China"},
    "Moscow": {"Capital": "Russia"},
    "Paris": {"Capital": "France"},
}
for b in sorted(a.keys()):
    c = a[b]["Capital"];
    print("{} is the capital of {}!".format(b, c));

Python 解法, 执行用时: 10ms, 内存消耗: 2840KB, 提交时间: 2022-07-14

cities_dict = {'Beijing':{'Capital': 'China'},'Moscow':{'Capital': 'Russia'},'Paris':{'Capital': 'France'}}
for key in sorted(cities_dict):
    aaa = cities_dict[key]
    print('{} is the capital of {}!'.format(key,aaa['Capital']))

Python 解法, 执行用时: 10ms, 内存消耗: 2840KB, 提交时间: 2022-07-08

cities_dict = {'Beijing':{'Capital':"China"},'Moscow':{'Capital':'Russia'},'Paris':{'Capital':'France'}}
for i in sorted(cities_dict):
    for j in cities_dict[i]:
        print("{} is the capital of {}!".format(i, cities_dict[i][j]))

                                                                                    
                                                                                    
                                                                                    

Python 解法, 执行用时: 10ms, 内存消耗: 2844KB, 提交时间: 2022-06-23

cities_dict = {'Beijing': {'capital': 'China'},'Moscow': {'capital': 'Russia'},'Paris': {'capital': 'France'}}
for i in sorted(cities_dict):
    print('%s is the capital of %s!'%(i,cities_dict[i]['capital']))

上一题