题解 | #遍历字典#
遍历字典
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}.")
天翼支付科技有限公司公司福利 16人发布