当前位置:网站首页>字典常用方法
字典常用方法
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~
边栏推荐
- Golang分布式应用之定时任务
- Power button 1374. Generate each character string is an odd number
- The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
- AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
- Chengdu openGauss user group recruit!
- Win Go development kit installation configuration, GoLand configuration
- Golang分布式应用之Redis
- BI - SQL 丨 WHILE
- 个人博客系统项目测试
- Install mysql using docker
猜你喜欢
Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
Redis 底层的数据结构
Unable to log in to the Westward Journey
oracle查询扫描全表和走索引
nacos启动报错,已配置数据库,单机启动
Golang分布式应用之定时任务
2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
AI target segmentation capability for fast video cutout without green screen
随机推荐
The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
AntPathMatcher uses
『网易实习』周记(一)
垃圾回收器CMS和G1
PHP live source code to achieve simple barrage effect related code
【LeetCode Daily Question】——704. Binary Search
¶ Backtop back to the top is not effective
Personal blog system project test
leetcode / anagram in string - some permutation of s1 string is a substring of s2
使用DBeaver进行mysql数据备份与恢复
oracle查询扫描全表和走索引
Fly propeller power space future PIE - Engine Engine build earth science
Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
Analysis of the status quo of digital transformation of manufacturing enterprises
记一次gorm事务及调试解决mysql死锁
oracle query scan full table and walk index
Hiring a WordPress Developer: 4 Practical Ways
2022-08-01 安装mysql监控工具phhMyAdmin
Force buckle, 752-open turntable lock
【web】理解 Cookie 和 Session 机制