python 循环字典
时间: 2023-08-29 07:11:39 浏览: 83
python字典
在Python中,可以使用循环遍历字典的键、值或者键值对。下面是几种常见的循环字典的方式:
1. 遍历键:
```python
my_dict = {"a": 1, "b": 2, "c": 3}
for key in my_dict:
print(key)
```
输出:
```
a
b
c
```
2. 遍历值:
```python
my_dict = {"a": 1, "b": 2, "c": 3}
for value in my_dict.values():
print(value)
```
输出:
```
1
2
3
```
3. 遍历键值对:
```python
my_dict = {"a": 1, "b": 2, "c": 3}
for key, value in my_dict.items():
print(key, value)
```
输出:
```
a 1
b 2
c 3
```
以上是几种常见的循环字典的方式,你可以根据具体需求选择适合的方法。
阅读全文