当前位置:网站首页>字典常用方法
字典常用方法
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~
边栏推荐
- leetcode/字符串中的变位词-s1字符串的某个排列是s2的子串
- The failure to create a role in Dahua Westward Journey has been solved
- 【Unity入门计划】2D Game Kit:初步了解2D游戏组成
- "NetEase Internship" Weekly Diary (3)
- [LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
- 力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
- 『网易实习』周记(三)
- Redis Persistence - RDB and AOF
- MySQL - CRUD operations
- MySQL优化策略
猜你喜欢

"NetEase Internship" Weekly Diary (1)

Hash collisions and consistent hashing

nacos startup error, the database has been configured, stand-alone startup

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

Personal blog system project test

Use DBeaver for mysql data backup and recovery

AWR分析报告问题求助:SQL如何可以从哪几个方面优化?

Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!

Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles

Win Go development kit installation configuration, GoLand configuration
随机推荐
Effects of Scraping and Aggregation
TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
2022-08-01 mysql/stoonedb慢SQL-Q18分析
The underlying data structure of Redis
Centos7 安装postgresql并开启远程访问
Handwriting a blogging platform ~ Day 3
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?
LeetCode刷题日记:153、寻找旋转排序数组中的最小值
Ask God to answer, how should this kind of sql be written?
个人博客系统项目测试
libcurl访问url保存为文件的简单示例
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
Check if IP or port is blocked
Constructor instance method inheritance of typescript37-class (extends)
项目后台技术Express
Golang分布式应用之Redis
"NetEase Internship" Weekly Diary (1)
volatile原理解析
LeetCode brush diary: LCP 03. Machine's adventure