首页 > 试题广场 >

遍历字典

[编程题]遍历字典
  • 热度指数: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确实新增了一对键-值对。

输入描述:


输出描述:
按题目描述进行输出即可(注意前后两个输出部分需以一个空行进行分隔)。
头像 小伙子66
发表于 2022-06-10 16:06:46
operator_dict={'<':'less than','==':'equal'} print('Here is the original dict:') for x in sorted(operator_dict): print(f'Operator {x} means {ope 展开全文
头像 👿测试实习
发表于 2023-12-05 16:28:17
#不好意思,我看没人吐槽这题目描述,那只能我来了,这题描述就是一陀答辩 operators_dict = {'<':'less than', '==':'equal'} print('Here is the original dict:') for i in sorted(operators_ 展开全文
头像 shenggong
发表于 2022-06-02 17:53:16
operators_dict={'<': 'less than','==': 'equal'} print('Here is the original dict:') for k,v in sorted(operators_dict.items()):     print( 展开全文
头像 Austin_zhai
发表于 2022-06-23 16:28:06
创建字典operators_dict operators_dict = {'<': 'less than', '==': 'equal'} 遍历排序后的字典进行信息打印 print('Here is the original dict:') for m, n in sorted(operat 展开全文
头像 Danzo123
发表于 2022-08-19 16:34:55
这个题目描述的真是够呛,理解个半天 def printOpKV(opDict):     opKeys= sorted(operators_dict.keys())     for k&nb 展开全文
头像 牛客807406307号
发表于 2022-12-18 01:44:09
# 字典的赋值操作 operators_dict ={ '<': 'less than', '==': 'equal' } print('Here is the original dict:') # dict.keys() 可以返回字典所有的键 # dict.values() 展开全文
头像 牛客277483075号
发表于 2022-06-03 20:32:26
operators_dict={'<':'less than','==':'equal'} print('Here is the original dict:') for key,val in sorted(operators_dict.items()): print(f'Operator { 展开全文
头像 牛客422438404号
发表于 2022-07-14 12:17:39
#step1:创建字典 #step2:sorted()函数排序 #step3:添加新元素 #step4:通过键遍历字典 operators_dict={'<': 'less than','==': 'equal'} print('Here&n 展开全文
头像 小龙哒哒
发表于 2022-09-16 16:22:03
严格按照题目要求去做,另外记得字典排序的时候默认是keys排序,获取value的时候可以使用dict.get(key)也可以直接dict[key]。 operators_dict = {'<': 'less than','==': 'equ 展开全文
头像 在思考的六边形战士很想去旅行
发表于 2025-09-23 19:27:15
# dict[key1] == value1 # sorted(iterable,reverse:bool=False)方法,默认按升序给可迭代对象排序,返回列表; # 如果可迭代对象是字典,对字典的键排序,返回的列表里也只含字典的键 operator_dict = {"<" 展开全文

问题信息

上传者:牛客301499号
难度:
166条回答 1426浏览

热门推荐

通过挑战的用户

遍历字典