首页 > 试题广场 >

遍历字典

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

输入描述:


输出描述:
按题目描述进行输出即可(注意前后两个输出部分需以一个空行进行分隔)。
没10年脑血栓出不出来的题
发表于 2022-09-01 13:34:27 回复(18)
# 创建一个字典 operators_dict
operators_dict = {'<': 'less than','==': 'equal'}

# 先打印一行
print('Here is the original dict:')
# 在使用 for 循环 遍历 使用 sorted 函数 排序 包含 operators_dict 所有键值对的列表
for key, value in sorted(operators_dict.items()):
    # 输出类似字符串
    print(f'Operator {key} means {value}.')

# 增加键值对
operators_dict['>'] = 'greater than'
# 输出一个换行
print()
# 在打印一行字符串
print('The dict was changed to:')
# 再来个和上述一样的for循环
for key, value in sorted(operators_dict.items()):
    print(f'Operator {key} means {value}.')
发表于 2022-07-12 14:59:53 回复(5)
突出一个,需求不清晰。讲不明白要干啥。建议重新出一遍题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
d = {'<':'less than', '==':'equal'}
print('Here is the original dict:')
forx,y in sorted(d.items()):
    print(f'Operator {x} means {y}.')
d.setdefault('>', 'greater than')
print()
print('The dict was changed to:')
forx,y in sorted(d.items()):
    print(f'Operator {x} means {y}.')
# print('Operator < means less than.')
# d.setdefault('>', 'greater than')
# print(d)
# n = sorted(d)
# fori in n:
#     print(i)
# print('The dict was changed to:')

发表于 2022-09-08 22:40:53 回复(0)
感觉没问题啊
operators_dict={'<':'less than','==':'equal'}
print('Here is the original dict:')
for k,v in sorted(operators_dict.items()):
    print(f'Operators {k} means {v}')
operators_dict['>']='greater than'
print()
print('The dict was changed to:')

for k,v in sorted(operators_dict.items()):
    print(f'Operators {k} means {v}')

发表于 2022-06-19 22:33:20 回复(7)
operators_dict = {'<': 'less than', '==': 'equal'}

print('Here is the original dict:')
sorted(operators_dict)
for x, y in operators_dict.items():
    print(f'Operator {x} means {y}.')
operators_dict['>'] = 'greater than'

print()
# 输出一个换行!!!print('\n')输出两个换行

print('The dict was changed to:')
sorted(operators_dict)
for x, y in operators_dict.items():
    print(f'Operator {x} means {y}.')

发表于 2022-09-27 12:38:59 回复(0)
dict={'<':'less than','==':'equal'}
print('Here is the original dict:')
for i in sorted(dict):
    print('Operator %s means %s.'%(i,dict[i]))
dict['>']='greater than'
print('\nThe dict was changed to:')
for i in sorted(dict):
    print('Operator %s means %s.'%(i,dict[i]))

发表于 2022-08-25 10:31:13 回复(0)
这题目绕的真让人头疼,哪个神仙出的啊?很多人读不懂吧,浪费好多时间读题
还不就是创建一个字典,输出临时排序的字典内容,再往字典中新增键值对,再输出临时排序的最新的字典内容

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)
operators_dict = {'<': 'less than' , '==': 'equal'}
print('Here is the original dict:')
# 对字典key升序排序--
my_list = sorted(operators_dict)
for i in my_list:
    value = operators_dict[i]
    print(f'Operator {i} means {value}.')
# 这里要输出一个空行,易错
print('')
# 字典新增元素---字典名[key]=value。就表示新增,因为字典的key是唯一的
operators_dict['>'] = 'greater than'

print('The dict was changed to:')
# 再次遍历列表
my_list_new = sorted(operators_dict)
for i in my_list_new:
    value = operators_dict[i]
    print('Operator {} means {}.'.format(i,value))

发表于 2022-06-26 23:24:30 回复(0)
operators_dict={'<': 'less than','==': 'equal'}
print('Here is the original dict:')
l1=sorted(operators_dict)
for i in l1:
    print('Operator '+i+' means '+operators_dict[i]+'.')
operators_dict['>']='greater than'
print(sep='/')
print('The dict was changed to:')
l2=sorted(operators_dict)
for i in l2:
    print('Operator '+i+' means '+operators_dict[i]+'.')
感觉题目好像是要求只遍历键(key)
发表于 2022-08-16 11:00:48 回复(0)
operators_dict = {'<': 'less than', '==': 'equal'}
print('Here is the original dict:')
def explain():
    for i,j in sorted(operators_dict.items()):
        print('Operator %s means %s.' % (i,j))
        
explain()    
operators_dict['>'] = 'greater than'
print()
print('The dict was changed to:')

explain()
发表于 2022-07-11 16:51:34 回复(0)
operators_dict={'<': 'less than','==': 'equal'}
print("Here is the original dict:")
operators_dict=sorted(operators_dict)
for key,value in operators_dict.items():
    print(f"Operator{key} means {value}")
为什么这样不行啊
operators_dict['>']='greater than'
print()
print("The dict was changed to:")
for key,value in operators_dict.items():
    print(f"Operator{key} means {value}")
发表于 2023-11-16 16:18:46 回复(0)
operators_dict = {"<": "less than", "==": "equal"}
print("Here is the original dict:")
for i in sorted(operators_dict.keys()):
    print("Operator {} means {}.".format(i, operators_dict[i]))

operators_dict.update({">": "greater than"})
print()
print("The dict was changed to:")
for i in sorted(operators_dict.keys()):
    print("Operator {} means {}.".format(i, operators_dict[i]))

发表于 2023-09-08 22:19:49 回复(0)
operators_dict = {"<": "less than", "==": "equal"}
print("Here is the original dict:")
for k, l in sorted(operators_dict.items()):
    print(f"Operator {k} means {l}.")
operators_dict[">"] = "greater than"
print()

print("The dict was changed to:")
for f, g in sorted(operators_dict.items()):
    print(f"Operator {f} means {g}.")
发表于 2023-03-03 10:40:00 回复(0)
出题人会中文吗?这是人说出来的话吗?
发表于 2026-05-26 14:52:11 回复(0)
看完题目描述,第一反应:??考语文的阅读理解吗,火速来到讨论区验证猜想,证毕
发表于 2026-04-08 16:12:50 回复(0)
脑瘫题目:
operators_dict = {"<":"less than","==":"equal"}
print("Here is the original dict:")
for operators in sorted(operators_dict):
    print(f"Operator {operators} means {operators_dict[operators]}.")
operators_dict['>'] = 'greater than'
print()
print('The dict was changed to:')
for operators in sorted(operators_dict):
    print(f"Operator {operators} means {operators_dict[operators]}.")


发表于 2026-03-20 10:28:52 回复(0)
operators_dict = {"<":"less than","==":"equal"}
print("Here is the original dict:")
for i in sorted(operators_dict):
    print(f"Operator {i} means {operators_dict[i]}.")
operators_dict.setdefault(">", "greater than")
print("\nThe dict was changed to:")
for i in sorted(operators_dict):
    print(f"Operator {i} means {operators_dict[i]}.")
发表于 2026-03-17 09:34:41 回复(0)
这题题目有错,半天没通过,最后新增的值应该是greater than
以及print()是默认换行,print('\n')会连换两行
operators_dict = {'<': 'less than','==': 'equal'}
print('Here is the original dict:')
for i in sorted(operators_dict):
    print(f'Operator {i} means {operators_dict[i]}.')

operators_dict['>'] = 'greater than'

print()
print('The dict was changed to:')
for i in sorted(operators_dict):
    print(f'Operator {i} means {operators_dict[i]}.')


发表于 2026-02-19 08:58:54 回复(0)
看完题目都不想去做了,理解了个大半天,哪个神仙出这种脑泡的描述
发表于 2026-02-02 15:10:55 回复(0)
找半天问题,结果发现>是
"greater than",题目给的
"less than"
发表于 2026-01-18 14:27:56 回复(0)

问题信息

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

热门推荐

通过挑战的用户

遍历字典