当前位置:网站首页>字典常用方法
字典常用方法
2022-08-02 02:18:00 【honker707】
作者简介:大家好我是hacker707,大家可以叫我hacker
个人主页:hacker707的csdn博客
系列专栏:python基础教程
推荐一款模拟面试、刷题神器点击跳转进入网站
python基础之字典常用方法
持续更新python基础知识,欢迎各位来访,一起交流学习python~
字典
字典简介
1.字典的概念
字典和列表类似,也是可变序列,不过和列表不同,它是无序的可变序列,保存的内容是以键值对(key:value)形式存放的
字典的每个键值之间用冒号:分隔,每个键值对之间用,隔开,整个字典包含在{ }中
dict = {
key1:value1,key2:value2}
1.字典的主要特征
1:通过键而不是通过索引来读取
2:字典是任意对象的无序集合
3:字典是可变的,可以随意嵌套
4:字典的键必须唯一
5:字典的键必须不可变
2.创建字典的三种方法
# 第一种方法
dic1 = {
'name':'hacker','age':'18'}
# 第二种方法
dic2 = dict(name='hacker',age='18')
# 第三种方法
dic3 = dict([('name','hacker'),('age','18')])
字典常用方法
1.clear()
定义 clear()方法清空字典中的所有元素(返回空字典)
举个栗子清空car字典中的所有元素
car = {
"brand": "Porsche", "model": "911", "year": 1963}
car.clear()
print(car)
运行结果如下:
{
}
2.copy()
定义 copy()方法返回字典的副本(复制字典)
举个栗子复制car字典
car = {
"brand": "Porsche", "model": "911", "year": 1963}
res = car.copy()
print(res)
运行结果如下
{
'brand': 'Porsche', 'model': '911', 'year': 1963}
3.get()
定义 get()方法返回指定键的值
举个栗子使用get方法返回"model"的值
car = {
"brand": "Porsche", "model": "911", "year": 1963}
x = car.get("model")
print(x)
运行结果如下:
911
4.keys()
定义返回字典里的所有键
举个栗子返回car字典的所有键
car = {
"brand": "Porsche", "model": "911", "year": 1963}
res = car.keys()
print(res)
运行结果如下:
dict_keys(['brand', 'model', 'year'])
5.values()
定义 返回字典的所有值
举个栗子返回car字典的所有值
car = {
"brand": "Porsche", "model": "911", "year": 1963}
res = car.values()
print(res)
运行结果如下:
dict_values(['Porsche', '911', 1963])
6.items()
定义返回字典的所有键值对
举个栗子返回car字典的所有键值对
car = {
"brand": "Porsche", "model": "911", "year": 1963}
res = car.items()
print(res)
运行结果如下:
dict_items([('brand', 'Porsche'), ('model', '911'), ('year', 1963)])
7.del()
定义 删除字典元素
举个栗子删除car字典的"model"的键
car = {
"brand": "Porsche", "model": "911", "year": 1963}
del car["model"]
print(car)
运行结果如下:
{
'brand': 'Porsche', 'year': 1963}
8.zip()
定义 zip()方法将键值打包成一个字典
li1 = ["name","age"]
li2 = ["hacker","18"]
print(dict(zip(li1,li2)))
运行结果如下:
{
'name': 'hacker', 'age': '18'}
以上就是字典常用的方法整理,如果有改进的建议欢迎私信或者在评论区留言奥~
欢迎各位来访,一起交流学习python~
边栏推荐
- oracle查询扫描全表和走索引
- CodeTon Round 2 D. Magical Array
- 十字光标太小怎么调节、CAD梦想画图算量技巧
- 2022-08-01 Install mysql monitoring tool phhMyAdmin
- FOFAHUB使用测试
- From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
- Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
- 哈希冲突和一致性哈希
- [LeetCode Daily Question]——654. The largest binary tree
- Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
猜你喜欢

永磁同步电机36问(二)——机械量与电物理量如何转化?

Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021: Interpretation

How to adjust the cross cursor too small, CAD dream drawing calculation skills

Win Go development kit installation configuration, GoLand configuration

使用DBeaver进行mysql数据备份与恢复

记一次gorm事务及调试解决mysql死锁

2022-07-30 mysql8执行慢SQL-Q17分析

项目后台技术Express

Outsourcing worked for three years, it was abolished...
![[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree](/img/b9/35813ae2972375fa728e3c11fab5d3.png)
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
随机推荐
记一个gorm初始化的坑
Handwriting a blogging platform ~ the first day
2022-08-01 反思
The state status is displayed incorrectly after the openGauss switch
Redis 底层的数据结构
AI目标分割能力,无需绿幕即可实现快速视频抠图
[LeetCode Daily Question]——654. The largest binary tree
LeetCode刷题日记:153、寻找旋转排序数组中的最小值
『网易实习』周记(二)
拼多多借力消博会推动国内农产品品牌升级 看齐国际精品农货
Remember a gorm transaction and debug to solve mysql deadlock
Use baidu EasyDL implement factory workers smoking behavior recognition
项目后台技术Express
Win Go development kit installation configuration, GoLand configuration
Coding Experience Talk
Golang分布式应用之Redis
垃圾回收器CMS和G1
Outsourcing worked for three years, it was abolished...
Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
MySQL - CRUD operations