python字典更新
时间: 2024-01-19 14:18:46 浏览: 89
以下是三种Python字典更新的方法:
1. 使用索引方式更新字典
```python
smart_girl = {"name": "Alice", "age": 35}
smart_girl["age"] = 50
print(smart_girl) # 输出:{"name": "Alice", "age": 50}
```
2 使用`update`方法更新字典
```python
smart_girl = {"name": "Alice", "age": 35}
smart_girl.update({"age": 50})
print(smart_girl) # 输出:{"name": "Alice", "age": 50}
```
3. 使用`update`方法和解包字典更新字典
```python
smart_girl = {"name": "Alice", "age": 35}
smart_girl.update(**{"age": 50})
print(smart_girl) # 输出:{"name": "Alice", "age": 50}
```
阅读全文