再使用for循环遍历 已使用sorted()函数按升序进行临时排序的包含字典operators_dict的所有键的列表,使用print()语句一行输出类似字符串'Operator < means less than.'的语句;
对字典operators_dict增加键-值对'>': 'greater than'后,
输出一个换行,再使用print()语句一行打印字符串'The dict was changed to:',
再次使用for循环遍历 已使用sorted()函数按升序进行临时排序的包含字典operators_dict的所有键的列表,使用print()语句一行输出类似字符串'Operator < means less than.'的语句,确认字典operators_dict确实新增了一对键-值对。
输入描述:
无
输出描述:
按题目描述进行输出即可(注意前后两个输出部分需以一个空行进行分隔)。
手把手带你学python—牛客网python基础 遍历字典
解题思路:
operators_dict = {'<': 'less than','==': 'equal'}
print('Here is the original dict:')
for i in sorted(operators_dict.keys(),reverse=False):
print('Operator %s means %s.'%(i,operators_dict.get(i)))
operators_dict[">"]="greater than"
print()
print('The dict was changed to:')
for i in sorted(operators_dict.keys(),reverse=False):
print('Operator %s means %s.'%(i,operators_dict.get(i)))
代码解析:
operators_dict = {'<': 'less than','==': 'equal'}
print('Here is the original dict:')
for i in sorted(operators_dict.keys(),reverse=False):
print('Operator %s means %s.'%(i,operators_dict.get(i)))
operators_dict[">"]="greater than"
print()
print('The dict was changed to:')
for i in sorted(operators_dict.keys(),reverse=False):
print('Operator %s means %s.'%(i,operators_dict.get(i)))