当前位置:网站首页>字典常用方法
字典常用方法
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~
边栏推荐
- ALCCIKERS Shane 20191114
- FOFAHUB usage test
- 拼多多借力消博会推动国内农产品品牌升级 看齐国际精品农货
- Redis 底层的数据结构
- LeetCode brushing diary: 33. Search and rotate sorted array
- oracle查询扫描全表和走索引
- 用位运算为你的程序加速
- Rasa 3 x learning series - Rasa - 4873 dispatcher Issues. Utter_message study notes
- Fundamentals of Cryptography: X.690 and Corresponding BER CER DER Encodings
- LeetCode Review Diary: 34. Find the first and last position of an element in a sorted array
猜你喜欢

Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案

Analysis of volatile principle
![[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games](/img/8a/07ca69c6dcc22757156cb615e241f8.png)
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games

LeetCode刷题日记:LCP 03.机器人大冒险

记一个gorm初始化的坑

The Paddle Open Source Community Quarterly Report is here, everything you want to know is here

Hiring a WordPress Developer: 4 Practical Ways

Check if IP or port is blocked

【LeetCode每日一题】——654.最大二叉树

The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
随机推荐
LeetCode brush diary: LCP 03. Machine's adventure
[LeetCode Daily Question]——654. The largest binary tree
Service discovery of kubernetes
Handwriting a blogging platform ~ Day 3
AWR analysis report questions for help: How can SQL be optimized from what aspects?
记一次gorm事务及调试解决mysql死锁
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
C language inserted into the characters of simple exercises
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
[ORB_SLAM2] SetPose, UpdatePoseMatrices
Remember a pit for gorm initialization
Project Background Technology Express
openGauss切换后state状态显示不对
Centos7 install postgresql and enable remote access
Fundamentals of Cryptography: X.690 and Corresponding BER CER DER Encodings
软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言
2022-08-01 mysql/stoonedb慢SQL-Q18分析
力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
2022-08-01 Reflection
项目后台技术Express