dict中所有方法的使用

简介: 提示:以下是本篇文章正文内容,下面案例可供参考

前言

dict中所有方法的使用


提示:以下是本篇文章正文内容,下面案例可供参考


1、clear

Remove all items from D. """
# 从 D 中删除所有项目

样例:

dict_var = {1: 2, 2: 3}
print(dict_var, type(dict_var))
dict_var.clear()
print(dict_var)
# 输出
{1: 2, 2: 3} <class 'dict'>
{}

2、copy

D.copy() -> a shallow copy of D 
# D的浅副本

样例:

dict_var = {1: 2, 2: 3}
print(dict_var)
dict_var1 = dict_var.copy()
print(dict_var1)
# 输出
{1: 2, 2: 3}
{1: 2, 2: 3}

3、fromkeys

Create a new dictionary with keys from 
iterable and values set to value.
# 创建一个新字典,其中包含可迭代的键并将值设置为值。

样例:

dict_var = {1: 2, 2: 3}
return_value = dict_var.get(1)
print(return_value)
return_value = dict_var.get(3)
print(return_value)
# 输出
2
None

4、get

Return the value for key if key is in the dictionary, else default. 
# 如果 key 在字典中,则返回 key 的值,否则默认

样例:

dict_var = {1: 2, 2: 3}
return_value = dict_var.get(1)
print(return_value)
return_value = dict_var.get(3)
print(return_value)
# 输出
2
None

5、items

a set-like object providing a view on D's items 
# 一个类似集合的对象,提供 D 项的视图

样例:

dict_var = {1: 2, 2: 3}
return_value = dict_var.items()
print(return_value)
# 输出
dict_items([(1, 2), (2, 3)])

6、keys

a set-like object providing a view on D's keys 
# 一个类似集合的对象,提供 D 键上的视图

样例:

dict_var = {1: 2, 2: 3}
return_value = dict_var.keys()
print(return_value)
# 输出
dict_keys([1, 2])

7、pop

remove specified key and return the corresponding value.
If key is not found, d is returned if given, 
otherwise KeyError is raised
# 删除指定的键并返回相应的值。
#  如果未找到 key,则返回 d(如果给定),否则引发 KeyError

样例:

dict_var = {1: 2, 2: 3}
return_value = dict_var.pop(1)
print(return_value)
return_value = dict_var.pop(0)
print(return_value)
# 输出
2
KeyError: 0 # 未找到值

8、popitem

Remove and return a (key, value) pair as a 2-tuple.
pairs are returned in LIFO (last-in, first-out) order.
Raises KeyError if the dict is empty.
# 删除(键、值)对并将其作为 2 元组返回。
# 成对按后进先出(后进先出)顺序返回。
# 如果字典为空,则引发 KeyError。

样例:

dict_var = {1: 2, 3: 4, 4: 5}
return_value = dict_var.popitem()
print(return_value)
print(dict_var)
# 输出
(4, 5)
{1: 2, 3: 4}

9、setdefau

Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
# 如果键不在字典中,则插入值为默认值的键。
#  如果 key 在字典中,则返回 key 的值,否则返回默认值。

样例

dict_var = {1: 2, 2: 3}
return_value = dict_var.setdefault(1)
print(return_value)
return_value = dict_var.setdefault(3)
print(return_value)
# 输出
2
None

10、update

If E is present and has a .keys() method,
then does:  for k in E: D[k] = E[k]
If E is present and lacks a .keys() method, then does
: for k, v in E: D[k] = v
In either case, this is followed by: for k in F:  D[k] = F[k]
# 从字典/可迭代 E 和 F 更新 D。
# 如果 E 存在并且具有 .keys() 方法,则对于 E 中的 k:D[k] = E[k]
# 如果 E 存在并且缺少 .keys() 方法,则对于 E 中的 k, v:D[k] = v
# 在任何一种情况下,这后面跟着:对于F中的k:D[k] = F[k]

样例:

dict_var = {1: 2, 3: 4}
print(dict_var)
dict_var.update({5: 5})
print(dict_var)
dict_var.update([(0, 0)])
print(dict_var)
dict_var.update(one=3, two=3)
print(dict_var)
# 输出
{1: 2, 3: 4}
{1: 2, 3: 4, 5: 5}
{1: 2, 3: 4, 5: 5, 0: 0}
{1: 2, 3: 4, 5: 5, 0: 0, 'one': 3, 'two': 3}

11、values

an object providing a view on D's values
#提供 D 值视图的对象

样例:

dict_var = {1: 2, 5: 3}
return_value = dict_var.values()
print(return_value)
dict_values([2, 3])


目录
打赏
0
0
0
0
1
分享
相关文章
数据集加载时报错'dict' object has no attribute 'requests‘
数据集加载时报错'dict' object has no attribute 'requests‘
365 5
|
8月前
dict 和 set 的 8 个经典使用例子
dict 和 set 的 8 个经典使用例子
71 4
【Tensorflow+keras】解决使用model.load_weights时报错 ‘str‘ object has no attribute ‘decode‘
python 3.6,Tensorflow 2.0,在使用Tensorflow 的keras API,加载权重模型时,报错’str’ object has no attribute ‘decode’
133 0
AttributeError: 'dict' object has no attribute 'has_key'
AttributeError: 'dict' object has no attribute 'has_key'
249 0
torch.split 的用法
这将返回一个元组,包含 3 个大小分别为 (6, 2)、(6, 2) 和 (6, 4) 的张量。 需要注意的是,当给定的拆分大小不等于张量在指定维度上的大小时,torch.split() 会引发一个异常。
571 0
Python编程:playhouse模块转peewee的model对象为字典dict
Python编程:playhouse模块转peewee的model对象为字典dict
485 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等