列表

详情


NP67. 遍历字典

描述

创建一个依次包含键-值对'<': 'less than'和'==': 'equal'的字典operators_dict,
先使用print()语句一行打印字符串'Here is the original dict:',
再使用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 解法, 执行用时: 10ms, 内存消耗: 2844KB, 提交时间: 2022-06-26

operators_dict = {
    '<': 'less than',
    '==': 'equal'
}

print ('Here is the original dict:')
for k in sorted(operators_dict.keys()):
    print ('Operator %s means %s.' %(k,operators_dict[k]))
operators_dict['>'] =  'greater than'
print ('')
print ('The dict was changed to:')
for k in sorted(operators_dict.keys()):
    print ('Operator %s means %s.' %(k,operators_dict[k]))

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

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

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

operators_dict = {'<': 'less than', '==': 'equal'}
print('Here is the original dict:')
for k,v in sorted(operators_dict.items()):
    print('Operator %s means %s.'%(k,v))
operators_dict['>'] = 'greater than'
print('')
print('The dict was changed to:')
for k,v in sorted(operators_dict.items()):
    print('Operator %s means %s.'%(k,v))

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

operators_dict={'<': 'less than','==': 'equal'}
print('Here is the original dict:')
list1=list(operators_dict.keys())
list1=sorted(list1)
for i in list1:
    print('Operator {} means {}.'.format(i,operators_dict[i]))
operators_dict['>']='greater than'
print('')
print('The dict was changed to:')
list2=list(operators_dict.keys())
list2=sorted(list2)
for i in list2:
    print('Operator {} means {}.'.format(i,operators_dict[i]))

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

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

上一题