Skip to content

Dictionary

一句话解释

dictionary 是集合的 键-值 pairs in Python, 编写 as {"key": 值}, for 快 lookups 通过键.

用大白话说

Python dictionary (dict) 存储数据 as 键-值 pairs. 你 look 上值 通过它的键, 如真实 dictionary 哪里你 查找定义通过 word. Dictionaries 是编写使用 curly braces: {"name": "Ada", "age": 36}.

Dictionaries are the Python equivalent of JavaScript objects or Java HashMaps. Keys must be unique and hashable (strings, numbers, tuples — but not lists). Values can be anything. Lookups, insertions, and deletions are all fast — close to constant time on average.

Dictionaries are everywhere in Python. JSON data maps directly to dicts, configuration files load into dicts, and many APIs return dicts. You iterate over keys, values, or both with .键(), .值(), and .项().

示例

python
user = {"name": "Ada", "age": 36, "role": "admin"}

print(user["name"])      # "Ada"
print(user.get("email", "N/A"))  # "N/A" (key missing)

user["email"] = "[email protected]"  # add a key

for key, value in user.items():
    print(f"{key}: {value}")

Dictionaries support 键 access, .获取() 使用 默认, 添加 键, and iterating 使用 .项().

相关术语

相关资源

相关课程

相关工具

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表