[Python3] 017 字典的内置方法
1. Python3 中如何查看 dict() 的内置方法
- help(dict()) / help(dict)
- dir(dict()) / dir(dict)
2. 少废话,上例子
(1) 清理大师 clear()
- 释义:删除字典中所有键值对
- 示例:
# 例1
d1 = {"one":1, "two":2, "three":3}
print(d1)
d1.clear()
print(d1)
- 运行结果
{'one': 1, 'two': 2, 'three': 3}
{}
(2) 拷贝君 copy()
- 释义:返回字典的浅拷贝
- 示例:
# 例2
d2_1 = {"one":1, "two":2, "three":3}
d2_2 = d2_1.copy()
print("d2_1 =", d2_1)
print("id(d2_1) =", id(d2_1))
print("d2_2 =", d2_2)
print("id(d2_2) =", id(d2_2))
- 运行结果
d2_1 = {'one': 1, 'two': 2, 'three': 3}
id(d2_1) = 2648942750168
d2_2 = {'one': 1, 'two': 2, 'three': 3}
id(d2_2) = 2648942630160
(3) get(key, default=None)
(4) items()
- 释义:返回由字典的键值对组成的元组格式
- 示例:
# 例3
d3 = {"one":1, "two":2, "three":3}
i3 = d3.items()
print(type(i3))
print(i3)
- 运行结果
<class 'dict_items'>
dict_items([('one', 1), ('two', 2), ('three', 3)])
(5) keys()
- 释义:返回一个由字典所有的键组成的结构
- 示例:
# 例4
d4 = {"one":1, "two":2, "three":3}
k4 = d4.keys()
print(type(k4))
print(k4)
- 运行结果
<class 'dict_keys'>
dict_keys(['one', 'two', 'three'])
(6) pop()
- 释义:
- 删除指定的键并返回相应的值
- 键不存在时,如果设置过返回值,则返回该值;否则,抛出异常 keyError
- 示例:
# 例5.1
d5 = {"one":1, "two":2, "three":3}
p5_1 = d5.pop("one")
p5_2 = d5.pop("four", 4)
print(p5_1)
print(p5_2)
print(d5)
- 运行结果
1
4
{'two': 2, 'three': 3}
# 例5.2
d5 = {"one":1, "two":2, "three":3}
p5_3 = d5.pop("ten")
- 运行结果
KeyError……'ten'
(7) popitem()
- 释义:
- 移除并返回一些(键、值)对作为2元组
- 如果d为空,则引发keyror。
- 示例:
# 例6.1
d6_1 = {"one":1, "two":2, "three":3}
p6 = d6_1.popitem()
print(p6_1)
key, value = d6_1.popitem()
print(key, "<-->", value)
- 运行结果
('three', 3)
two <--> 2
# 例6.2
d6_2 = {}
print(d6_2.popitem())
- 运行结果
KeyError: 'popitem(): dictionary is empty'
(8) setdefault(key, default=None)
- 释义:
- 键在字典中时,则返回该键对应的值
- 键不在字典中时,添加一组键值对,键为传入的键值;若传入了值,则值为该值,否则,值为默认值 None
- 示例:
# 例7
d7 = {"one":1, "two":2, "three":3}
print(d7.setdefault("one"))
print(d7.setdefault("four"))
print(d7)
print(d7.setdefault("five", 5))
print(d7)
- 运行结果
1
None
{'one': 1, 'two': 2, 'three': 3, 'four': None}
5
{'one': 1, 'two': 2, 'three': 3, 'four': None, 'five': 5}
(9) update()
- 释义:
- 形如 dict1.update(dict2)
- 把字典 dict2 的 key/value 更新到字典 dict1 里
- 示例:
# 例8.1
dict8_1 = {"one":1, "two":2, "three":3}
dict8_2 = {"four":4}
dict8_1.update(dict8_2)
print ("new dict:", dict8_1)
- 运行结果
new dict: {'one': 1, 'two': 2, 'three': 3, 'four': 4}
# 例8.2
dict8_3 = {"one":1, "two":2, "three":3}
dict8_4 = {"two":2}
dict8_3.update(dict8_4) # 想更新的键值对为重复键值对
print ("new dict:", dict8_3)
- 运行结果
new dict: {'one': 1, 'two': 2, 'three': 3}
(10) values()
- 释义:与 keys() 相似,返回一个可迭代的结构
- 示例:
# 例9
d9 = {"one":1, "two":2, "three":3}
v9 = d9.values()
print(type(v9))
print(v9)
- 运行结果
<class 'dict_values'>
dict_values([1, 2, 3])
[Python3] 017 字典的内置方法的更多相关文章
- Python3.6 字典的内置方法
1.dict.clear(self) 删除字典内所有元素 2.dict.copy(self) 返回一个字典的浅复制,拷贝父对象,不会拷贝对象的内部的子对象 3.dict.fromkeys(self, ...
- what's the python之基本运算符及字符串、列表、元祖、集合、字典的内置方法
计算机可以进行的运算有很多种,运算按种类可分为算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.字符串和列表的算数运算只能用+和*,字典没有顺序,所以不能进行算数运算和比较运算.比较运算中= ...
- python之字典二 内置方法总结
Python字典包含了以下内置方法: clear()函数用于删除字典内所有元素 dict1 = {, 'Class': 'First'} print('the start len %d' % len( ...
- 元组/字典/集合内置方法+简单哈希表(day07整理)
目录 二十三.元组内置方法 二十四.字典数据类型 二十五 集合内置方法 二十五.数据类型总结 二十六.深浅拷贝 补充:散列表(哈希表) 二十三.元组内置方法 什么是元组:只可取,不可更改的列表 作用: ...
- [Python3] 014 集合的内置方法
目录 1. Python3 中如何查看 set() 的内置方法 2. 少废话,上例子 (1) add() (2) 又见清理大师 clear() (3) 又见拷贝君 copy() (4) 找茬君 dif ...
- day6 六、元组、字典、集合的基本操作和内置方法
一.元组 1.定义 # 元组tuple # 记录多个值,当值没有改的需求是,建议用元组更好 # 定义:在()内用逗号分开任意类型的值 # name = (, , 300.5]) # print(nam ...
- day07-列表类型/元组类型/字典类型/集合类型内置方法
目录 列表类型内置方法 元组类型内置方法 字典类型内置方法 集合类型内置方法 列表类型内置方法 用来存多个元素,[]内用逗号隔开任意数据类型的元素 1. list()强制类型转换 print(list ...
- day09-2 字典,集合的内置方法
目录 字典的内置方法 作用 定义方式 方法 优先掌握 需要掌握 存储一个值or多个值 有序or无序 可变or不可变 集合的内置方法 作用 定义方式 方法 存储一个值or多个值 有序or无序 可变or不 ...
- python之字符串,列表,字典,元组,集合内置方法总结
目录 数字类型的内置方法 整型/浮点型 字符串类型的内置方法 列表的内置方法 字典的内置方法 元组的内置方法 集合类型内置方法 布尔类型 数据类型总结 数字类型的内置方法 整型/浮点型 加 + 减 - ...
随机推荐
- 洛谷 P2704 [NOI2001]炮兵阵地 (状态压缩DP+优化)
题目描述 司令部的将军们打算在NM的网格地图上部署他们的炮兵部队.一个NM的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用"P" ...
- 如何获得带转义的json内容
stringify两次 JSON.stringify(JSON.stringify(obj))
- get请求和post请求参数中文乱码的解决办法
get请求参数中文乱码的解决办法 在tomcat的server.xml里的Connector加个URIEncoding="UTF-8",把 <Connector connec ...
- python如何调用c编译好可执行程序
python如何调用c编译好可执行程序 以下总结出几种在Python 中调用 C/C++ 代码的方法 ------------------------------------------- ...
- Java动手动脑02
一.平方数静方法: public class SquareInt { public static void main(String[] args) { int result; for (int x = ...
- Keras get Tensor dimensions
int_shape(y_true)[0] int_shape(y_true)[1]
- IIS部署复盘(杂记)
首先,230是网站服务器,231主要放到是数据库:所以在230(部署的服务器)上部署不需要部署IIS和Oracle数据库, 231呢?231是数据库服务器:百度一下数据库服务器是什么? 文档第五步: ...
- CF 480 B Long Jumps (map标记)
题目链接:http://codeforces.com/contest/480/problem/B 题目描述: Long Jumps Valery is a PE teacher at a ...
- CG-CTF | Hello,RE!
菜狗开始向着pwn与re进军了(●'◡'●)[说白了,还是在水博客吧] 按r出flag: galf leW{ emoc _oT_ W_ER dlro }! 反一下:fla ...
- Activity和Fragment生命周期对比
版权声明:本文为博主原创文章,未经博主允许不得转载.