当前位置:网站首页>字典常用方法
字典常用方法
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~
边栏推荐
- Handwriting a blogging platform ~ Day 3
- Chengdu openGauss user group recruit!
- 密码学的基础:X.690和对应的BER CER DER编码
- LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
- BI - SQL 丨 WHILE
- MySQL optimization strategy
- Redis for distributed applications in Golang
- Constructor instance method inheritance of typescript37-class (extends)
- Electronic Manufacturing Warehouse Barcode Management System Solution
- Centos7 安装postgresql并开启远程访问
猜你喜欢
【 wheeled odometer 】
Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
Hiring a WordPress Developer: 4 Practical Ways
Fly propeller power space future PIE - Engine Engine build earth science
Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
工程师如何对待开源
Electronic Manufacturing Warehouse Barcode Management System Solution
2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
个人博客系统项目测试
永磁同步电机36问(三)——SVPWM代码实现
随机推荐
记一次gorm事务及调试解决mysql死锁
The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
Check if IP or port is blocked
2022 Henan Youth Training League Game (3)
MySQL - CRUD operations
使用docker安装mysql
swift project, sqlcipher3 -> 4, cannot open legacy database is there a way to fix it
LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
Personal blog system project test
A good book for newcomers to the workplace
Handwriting a blogging platform ~ Day 3
to-be-read list
LeetCode刷题日记:153、寻找旋转排序数组中的最小值
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
¶ Backtop back to the top is not effective
The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
哈希冲突和一致性哈希
Rasa 3.x 学习系列- Rasa - Issues 4873 dispatcher.utter_message 学习笔记
LeetCode Review Diary: 34. Find the first and last position of an element in a sorted array
Handwriting a blogging platform ~ the first day