python获取字典的键名
时间: 2023-08-12 20:33:42 浏览: 120
python 获取字典键值对的实现
可以使用字典的keys()方法来获取所有的键名,例如:
```python
my_dict = {"name": "Alice", "age": 25, "gender": "female"}
keys = my_dict.keys()
print(keys) # 输出 dict_keys(['name', 'age', 'gender'])
```
也可以将keys()方法返回的结果转换成列表类型,例如:
```python
my_dict = {"name": "Alice", "age": 25, "gender": "female"}
keys = list(my_dict.keys())
print(keys) # 输出 ['name', 'age', 'gender']
```
阅读全文