题解 | #遍历字典#

遍历字典

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

# 创建一个字典 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}.")

全部评论

相关推荐

零零幺零零幺:至少再做一个项目,然后猛投小厂,不然有点难
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务