python字典一个键多个值
时间: 2024-03-07 11:24:32 浏览: 77
在 Python 中,可以使用列表或集合来存储字典中的一个键对应的多个值。
使用列表:
```python
my_dict = {'key': ['value1', 'value2', 'value3']}
```
可以使用 append() 方法向列表中添加新的值:
```python
my_dict['key'].append('value4')
```
使用集合:
```python
my_dict = {'key': {'value1', 'value2', 'value3'}}
```
可以使用 add() 方法向集合中添加新的值:
```python
my_dict['key'].add('value4')
```
注意,使用集合时,值是无序的,且不允许重复。如果需要保持顺序并且允许重复值,应该使用列表。
阅读全文
相关推荐















