当前位置:网站首页>字典
字典
2022-07-02 18:37:00 【51CTO】
什么是字典
· python内置的数据结构之一,与列表一样是一个可变序列
· 以键值对的方式存储数据,字典是一个无序的序列(存储数据以哈希表的方式存储
· 字典示意图
scores = { '张三' : 100 , '李四' : 98 }
字典名 花括号 键 冒号 值 逗号
字典的实现原理
字典的实现原理与查字典类似,查字典实现根据部首或拼音查找相应的页码,Python中的字典是根据key查找value所在的位置。
字典的创建
1.使用花括号{}

2.使用内置函数dict()

3.空字典的创建

字典的常用操作
1.字典中元素的获取
1.使用中括号[],举例:scores['张三']
2.使用函数get(), 举例:scores.get('张三')

3.[]取值与使用get()取值的区别
(1)[]取值如果字典中不存在指定的key,抛出KeyError异常
(2)get()方法取值,如果字典中不存在指定的key,并不会抛出KeyError,而是返回None,可以通过参数设置默认的Value,以便指定的Key不存在时返回


2.key的判断

3.字典元素的删除

4.字典元素的新增

5.获取字典视图的三个方法
1.keys()-获取字典的所有的key
2.values()-获取字典中所有value
3.item()-获取字典中所有key-value对

6.字典元素的遍历

字典的特点
1.字典中的元素都是一个key-value对,key不允许重复,value可以重复
2.字典中的元素是无序的
3.字典中的key值必须是不可变对象
4.字典也可以根据需要动态地伸缩
5.子弹会浪费较大的内存,是一种使用空间换时间的数据结构

字典生成式
内置函数zip()
用于将可迭代的对象作为参数,将对象中对应的元素打包成一个元组,然后返回这些元组组成的列表

值得注意的是,items中的值有可能比prices中的元素数量多,或者相反,不过在生成的过程中,只会以元素较少的一方为基准生成字典。
此外,可以采用item.upper():prince将字母大写
边栏推荐
- Use cheat engine to modify money, life and stars in Kingdom rush
- 2022 compilation principle final examination recall Edition
- Py之interpret:interpret的简介、安装、案例应用之详细攻略
- Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
- AcWing 1126. 最小花费 题解(最短路—dijkstra)
- A4988驱动步进电机「建议收藏」
- Microservice technology - distributed global ID in high concurrency
- 思考变量引起的巨大变化
- MySQL table historical data cleaning summary
- Yunna | why use the fixed asset management system and how to enable it
猜你喜欢

Markdown基础语法
![[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)](/img/c1/a00425f2e1824a50644c8fbb15fe38.jpg)
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)

PHP-Parser羽毛球预约小程序开发require线上系统

Machine learning notes - time series prediction research: monthly sales of French champagne

云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统

Quanzhi A33 uses mainline u-boot

Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode

IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config

Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5

线程应用实例
随机推荐
MySQL
Excel finds the same value in a column, deletes the row or replaces it with a blank value
【ERP软件】ERP体系二次开发有哪些危险?
Chapter 7 - class foundation
AcWing 341. 最优贸易 题解 (最短路、dp)
Py之interpret:interpret的简介、安装、案例应用之详细攻略
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
AcWing 383. 观光 题解(最短路)
"Patient's family, please come here" reading notes
搭建主从模式集群redis
Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
开发固定资产管理系统,开发固定资产管理系统用什么语音
Codeworks round 802 (Div. 2) pure supplementary questions
453-atoi函数的实现
Getting started with typescript
Gmapping code analysis [easy to understand]
Golang:[]byte to string
Advanced performance test series "24. Execute SQL script through JDBC"