feat(python基础): 新增字典教学课程
This commit is contained in:
118
01_python基础/1_10_字典/dictionaries.py
Normal file
118
01_python基础/1_10_字典/dictionaries.py
Normal file
@@ -0,0 +1,118 @@
|
||||
# 第 1-10 课示例:字典
|
||||
#
|
||||
# 本文件使用 Agent 配置作为统一示例,演示字典的主要操作。
|
||||
# 示例无需用户输入,可以直接运行并对照注释观察结果。
|
||||
|
||||
|
||||
# 一、创建字典并读取字段
|
||||
agent_config = {
|
||||
"model": "gpt-5",
|
||||
"temperature": 0.7,
|
||||
"max_tokens": 2000,
|
||||
"enabled": True,
|
||||
}
|
||||
|
||||
print(f"完整配置:{agent_config}")
|
||||
print(f"配置字段数量:{len(agent_config)}")
|
||||
print(f"模型名称:{agent_config['model']}")
|
||||
|
||||
|
||||
# 二、使用 get() 安全读取
|
||||
# timeout 不存在,因此返回指定的默认值 30。
|
||||
timeout = agent_config.get("timeout", 30)
|
||||
print(f"超时时间:{timeout}")
|
||||
|
||||
|
||||
# 三、添加和修改字段
|
||||
agent_config["timeout"] = 60
|
||||
agent_config["temperature"] = 0.3
|
||||
print(f"添加和修改后的配置:{agent_config}")
|
||||
|
||||
|
||||
# 四、使用 update() 更新多个字段
|
||||
agent_config.update({
|
||||
"temperature": 0.2,
|
||||
"retries": 3,
|
||||
})
|
||||
print(f"批量更新后的配置:{agent_config}")
|
||||
|
||||
|
||||
# 五、删除字段
|
||||
# pop() 返回被删除的值;提供默认值可以避免键不存在时报错。
|
||||
removed_retries = agent_config.pop("retries", None)
|
||||
print(f"被删除的重试次数:{removed_retries}")
|
||||
|
||||
if "timeout" in agent_config:
|
||||
del agent_config["timeout"]
|
||||
|
||||
print(f"删除后的配置:{agent_config}")
|
||||
|
||||
|
||||
# 六、遍历键、值和键值对
|
||||
print("所有键:")
|
||||
for key in agent_config.keys():
|
||||
print(key)
|
||||
|
||||
print("所有值:")
|
||||
for value in agent_config.values():
|
||||
print(value)
|
||||
|
||||
print("所有键值对:")
|
||||
for key, value in agent_config.items():
|
||||
print(f"{key}:{value}")
|
||||
|
||||
|
||||
# 七、嵌套字典
|
||||
nested_config = {
|
||||
"model": {
|
||||
"name": "gpt-5",
|
||||
"temperature": 0.7,
|
||||
},
|
||||
"runtime": {
|
||||
"timeout": 30,
|
||||
"enabled": True,
|
||||
},
|
||||
}
|
||||
|
||||
print(f"嵌套模型名称:{nested_config['model']['name']}")
|
||||
print(f"嵌套超时时间:{nested_config['runtime']['timeout']}")
|
||||
|
||||
|
||||
# 八、列表中保存多个字典
|
||||
agents = [
|
||||
{"name": "代码助手", "model": "gpt-5", "enabled": True},
|
||||
{"name": "搜索助手", "model": "o3", "enabled": False},
|
||||
]
|
||||
|
||||
for agent in agents:
|
||||
# 使用已经学过的普通条件判断,把布尔值转换成更容易阅读的中文状态。
|
||||
if agent["enabled"]:
|
||||
status_text = "启用"
|
||||
else:
|
||||
status_text = "停用"
|
||||
|
||||
print(f"{agent['name']}:{agent['model']},状态:{status_text}")
|
||||
|
||||
|
||||
# 九、字典中保存列表
|
||||
agent_profile = {
|
||||
"name": "代码助手",
|
||||
"tools": ["搜索", "计算"],
|
||||
}
|
||||
agent_profile["tools"].append("写作")
|
||||
print(f"Agent 工具:{agent_profile['tools']}")
|
||||
|
||||
|
||||
# 十、直接赋值与复制
|
||||
original_config = {"model": "gpt-5", "enabled": True}
|
||||
alias_config = original_config
|
||||
alias_config["model"] = "o3"
|
||||
|
||||
print(f"直接赋值后的原字典:{original_config}")
|
||||
print(f"直接赋值后的别名字典:{alias_config}")
|
||||
|
||||
copied_config = original_config.copy()
|
||||
copied_config["model"] = "gpt-5"
|
||||
|
||||
print(f"复制后保持不变的原字典:{original_config}")
|
||||
print(f"修改后的副本:{copied_config}")
|
||||
Reference in New Issue
Block a user