python字典dict中常用内置函数的使用

作者:搬砖python中~ 时间:2022-09-06 02:10:09 

前言

字典是 Python 中很重要的数据类型,有很多内置函数需要了解。

1.dict.clear

清除字典中所有键值对。

dict = {'a':10, 'b':20, 'c':30}
dict.clear()
print(dict) # {}

2.dict.get

如果键存在于字典中,则返回该键的值。

如果未找到,则返回 None。

指定可选参数之后,未找到返回默认值。

dict = {'a':10, 'b':20, 'c':30}
print(dict.get('c')) # 30
print(dict.get('g')) # None
print(dict.get('g', -1)) # -1 指定可选参数

3.dict.items

返回字典中的键值对列表。

items() 返回包含键值对的元组列表。

每个元组中的第一项是键,第二项是键的值。

dict = {'a':10, 'b':20, 'c':30}
dict.items() # dict_items([('a', 10), ('b', 20), ('c', 30)])
list(dict.items()) # [('a', 10), ('b', 20), ('c', 30)]
list(dict.items())[1] # ('b', 20)
list(dict.items())[1][0] # 'b'

4.dict.keys

返回字典中的键列表。

dict = {'a':10, 'b':20, 'c':30}
dict.keys() # dict_keys(['a', 'b', 'c'])
list(dict.keys()) # ['a', 'b', 'c']

5.dict.values

返回字典中的值列表。

dict = {'a':10, 'b':20, 'c':30}
dict.values() # dict_values([10, 20, 30])
list(dict.values()) # [10, 20, 30]

# 即使值重复,也会被多次返回
dict2 = {'a':10, 'b':10, 'c':30}
list(dict2.values()) # [10, 10, 30]

6.dict.pop

从字典中删除一个键,如果它存在,并返回它的值。

如果不存在,则引发异常 KeyError。

指定可选参数,不存在时返回默认值,不引发异常。

dict = {'a':10, 'b':20, 'c':30}
dict.pop('b') # 20
print(dict) # {'a': 10, 'c': 30}
dict.pop('g')
'''
Traceback (most recent call last):

? File "<ipython-input-20-a81e983a7be0>", line 1, in <module>
? ? dict.pop('g')

KeyError: 'g'
'''
dict.pop('g', -1) # -1

7.dict.popitem

从字典中删除最后面的键值对,并返回。

直到字典被删除至空,则引发异常 KeyError。

dict = {'a':10, 'b':20, 'c':30}
dict.popitem() # ('c', 30)
print(dict) # {'a': 10, 'b': 20}
dict.popitem() # ('b', 20)
print(dict) # {'a': 10}
dict.popitem() # ('a', 10)
print(dict) # {}
dict.popitem()
'''
Traceback (most recent call last):

? File "<ipython-input-28-7e744445e3d2>", line 1, in <module>
? ? dict.popitem()

KeyError: 'popitem(): dictionary is empty'
'''

**注意:**在低于 3.6 的 Python 版本中,popitem( ) 将返回任意(随机)键值对,因为 Python 字典在 3.6 版本之前是无序的。

8.dict.update

将字典与另一个字典或可迭代的键值对合并。

dict = {'a':10, 'b':20, 'c':30}
dict2 = {'b':200, 'd':400}
dict.update(dict2)
print(dict) # {'a': 10, 'b': 200, 'c': 30, 'd': 400}

所有的值都被更新。

尾语 ??

来源:https://blog.csdn.net/weixin_62853513/article/details/130031007

标签:python,dict,内置函数
0
投稿

猜你喜欢

  • Python安装及Pycharm安装使用教程图解

    2023-08-01 05:38:06
  • python-opencv 将连续图片写成视频格式的方法

    2023-08-25 23:00:06
  • 使用css2.1实现多重背景、多重边框效果[译]

    2010-08-23 16:32:00
  • python目标检测yolo2详解及预测代码复现

    2021-09-30 12:35:20
  • python 使用matplotlib 实现从文件中读取x,y坐标的可视化方法

    2023-07-29 05:25:42
  • python 基于opencv实现图像增强

    2023-03-20 21:04:32
  • pytorch 实现二分类交叉熵逆样本频率权重

    2021-04-29 00:25:29
  • MySQL中delimiter的作用

    2010-10-25 20:26:00
  • Python爬虫简单运用爬取代理IP的实现

    2021-08-25 23:26:42
  • 如何由Sybase向SQL Server移植数据库

    2009-01-20 15:56:00
  • Python和C/C++交互的几种方法总结

    2021-08-25 00:49:06
  • ASP函数指针试探-GetRef

    2009-10-12 12:39:00
  • 利用python进行文件操作

    2022-01-09 10:49:51
  • PyTorch零基础入门之逻辑斯蒂回归

    2022-09-09 03:44:25
  • Pytorch之卷积层的使用详解

    2022-09-04 16:43:21
  • Python中使用第三方库xlutils来追加写入Excel文件示例

    2022-05-23 10:04:11
  • python 把列表转化为字符串的方法

    2023-06-14 20:35:48
  • Python中使用PyQt把网页转换成PDF操作代码实例

    2021-12-04 11:31:19
  • Python程序中的观察者模式结构编写示例

    2022-08-04 22:12:11
  • PyQt5创建一个新窗口的实例

    2021-12-31 02:49:44
  • asp之家 网络编程 m.aspxhome.com