题解 | #遍历字典#

遍历字典

https://www.nowcoder.com/practice/0315639767824323a2cdb9ee3f998314

# operators_dict = {'<': 'less than', '==': 'equal'}
# print('Here is the original dict:')
# # 这里遍历的是keys,通过索引找到相应的value
# for x in sorted(operators_dict):
#     print(f'Operator {x} means {operators_dict[x]}.')
# print(" ")
# operators_dict['>']='greater than'
# print("The dict was changed to:")
# for x in sorted(operators_dict):
#    print(f'Operator {x} means {operators_dict[x]}.')

operators_dict = {'<': 'less than', '==': 'equal'}
print('Here is the original dict:')
# 这里遍历的是keys,通过索引找到相应的value
for x, v in sorted(operators_dict.items()):
    print(f'Operator {x} means {v}.')
print(" ")
operators_dict['>']='greater than'
print("The dict was changed to:")
for x, v in sorted(operators_dict.items()):
   print(f'Operator {x} means {v}.')

Two methods to traverse the list.

全部评论

相关推荐

10-05 23:02
东北大学 Java
我说句实话啊:那时候看三个月培训班视频,随便做个项目背点八股,都能说3 40w是侮辱价
点赞 评论 收藏
分享
双非一本失业第二年:《机器视觉垃圾分类》
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务