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])


相关文章
|
10月前
|
存储 JSON 数据格式
数据集加载时报错'dict' object has no attribute 'requests‘
数据集加载时报错'dict' object has no attribute 'requests‘
216 5
映射setattr,getattr,delattr合集
映射setattr,getattr,delattr合集
|
3月前
|
存储 Python
Python中list, tuple, dict,set的区别和使用场景
Python中list, tuple, dict,set的区别和使用场景
|
4月前
|
存储 安全 Java
Python教程第3章 | 集合(List列表、Tuple元组、Dict字典、Set)
Python 列表、无序列表、字典、元组增删改查基本用法和注意事项
84 1
|
10月前
|
机器学习/深度学习 算法 Python
sklearn中的Bunch和dict的区别
sklearn中的Bunch和dict的区别
51 0
|
Python
AttributeError: 'dict' object has no attribute 'has_key'
AttributeError: 'dict' object has no attribute 'has_key'
134 0
|
Python
python中进一步理解字典,items方法、keys方法、values方法
python中进一步理解字典,items方法、keys方法、values方法
148 0
字典(dict)的遍历,就是将字典中的值都取出来。用到的方法主要有三个 keys()、values()、items()。
字典(dict)的遍历,就是将字典中的值都取出来。用到的方法主要有三个 keys()、values()、items()。
268 0
字典(dict)的遍历,就是将字典中的值都取出来。用到的方法主要有三个 keys()、values()、items()。
成功解决AttributeError: ‘dict_values‘ object has no attribute ‘index‘
成功解决AttributeError: ‘dict_values‘ object has no attribute ‘index‘