Python语言基础
Python语言进阶
Python数据结构

Python dictionary keys() 方法

搞懂Python dictionary keys() 方法的用法

描述

Python3 字典 keys() 方法返回一个可迭代对象,可以使用 list() 来转换为列表。
注意:Python2.x 是直接返回列表

语法

keys()方法语法:
dict.keys()

参数

NA。

返回值

返回一个迭代器。

实例

以下实例展示了 keys() 方法的使用方法:
>>> dict = {'Name': 'Lidihuo', 'Age': 2}
>>> dict.keys()
dict_keys(['Name', 'Age'])
>>> list(dict.keys()) # 转换为列表
['Name', 'Age']
>>>
以上实例输出结果为:
字典所有的键为 : dict_keys(['Age', 'Name'])
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4