Python - 字典方法



一个 Python 字典 是内置 dict 类的对象,它定义了以下方法:

字典方法

序号 方法和描述
1

dict.clear()

移除字典 dict 中的所有元素。

2

dict.copy()

返回字典 dict 的浅拷贝。

3

dict.fromkeys()

使用 seq 中的键创建一个新字典,并将值设置为 value。

4

dict.get(key, default=None)

对于键 key,返回 value,如果 key 不在字典中则返回 default。

5

dict.has_key(key)

如果字典中存在给定的键,则返回 True,否则返回 False。

6

dict.items()

返回字典 dict 的 (键,值) 元组对列表。

7

dict.keys()

返回字典 dict 的键列表。

8

dict.pop()

从集合中移除指定键的元素。

9

dict.popitem()

移除最后插入的键值对。

10

dict.setdefault(key, default=None)

类似于 get(),但如果 key 不在 dict 中,则会设置 dict[key]=default。

11

dict.update(dict2)

将字典 dict2 的键值对添加到 dict。

12

dict.values()

返回字典 dict 的值列表。

广告
© . All rights reserved.