首页 > 试题广场 >

遍历字典

[编程题]遍历字典
  • 热度指数:88001 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
创建一个依次包含键-值对'<': 'less than'和'==': 'equal'的字典operators_dict,
先使用print()语句一行打印字符串'Here is the original dict:',
再使用for循环遍历 已使用sorted()函数按升序进行临时排序的包含字典operators_dict的所有键的列表,使用print()语句一行输出类似字符串'Operator < means greater than.'的语句;

对字典operators_dict增加键-值对'>': 'less than'后,
输出一个换行,再使用print()语句一行打印字符串'The dict was changed to:',
再次使用for循环遍历 已使用sorted()函数按升序进行临时排的包含字典operators_dict的所有键的列表,使用print()语句一行输出类似字符串'Operator < means greater than.'的语句,确认字典operators_dict确实新增了一对键-值对。

输入描述:


输出描述:
按题目描述进行输出即可(注意前后两个输出部分需以一个空行进行分隔)。
这题目绕的真让人头疼,哪个神仙出的啊?很多人读不懂吧,浪费好多时间读题
还不就是创建一个字典,输出临时排序的字典内容,再往字典中新增键值对,再输出临时排序的最新的字典内容

a={'<':'less than','==':'equal'}
print('Here is the original dict:')
for i in sorted(a):
    print('Operator %s means %s.'%(i,a[i]))
b={'>':'greater than'}
a.update(b)
print('\nThe dict was changed to:')
for i in sorted(a):
    print('Operator %s means %s.'%(i,a[i]))

发表于 2022-08-27 19:20:26 回复(0)

问题信息

上传者:牛客301499号
难度:
1条回答 1425浏览

热门推荐

通过挑战的用户

遍历字典