字典解析:Python如何快速创建并操作字典示例
在Python中,创建并操作字典非常直观和快捷。以下是一些基本示例:
创建一个空字典:
dict_example = {}
创建一个包含键值对的字典:
dict_example = {
"name": "Alice",
"age": 30,
"city": "New York"
}
访问和修改字典中的值:
```python获取值
print(dict_example[“name”])) # 输出:Alice
修改值
dict_example[“age”] = 31
print(dict_example[“age”])) # 输出:31
删除键值对
if “city” in dict_example:
del dict_example[“city”]
print(dict_example) # 输出:{‘name’: ‘Alice’, ‘age’: 31}
```
以上就是Python快速创建并操作字典的示例。
还没有评论,来说两句吧...