✅作者简介:大家好我是Xlong,一枚正在学习COMSOL、Python的工科研究僧
📃个人主页: Xlong的个人博客主页
🔥系列专栏:Python学习
💖如果觉得博主的文章还不错的话,请👍支持一下博主哦🤞
本篇简介
在上一章《python基础(6):列表》中简单介绍了列表的相关知识。这一篇文章我们主要来学习python的内置数据结构之一:字典。
目录
一、什么是字典
(1)python内置的数据结构之一,与列表一样是一个可变序列。
(2)映射类型,以键-值对的方式存储数据,字典是一个无序的序列。
(3)键可以是数字、字符串、元组。就像英汉词典一样,可以通过查找某个特定的单词(键),来找到它的定义(值)。
scores = {'张三':100,'李四':98,'王五':45,}
print(scores) 运行结果:{'张三': 100, '李四': 98, '王五': 45}
二、字典的原理
2.1 字典的实现原理
(1)字典的实现原理与查字典类似,查字典是先根据部首或拼音查找对应的页码,python中的字典是根据key查找value所在的位置。
三、字典的创建与删除
3.1 字典的创建(最常用的方式)
(1)使用花括号:
#使用{}创建字典
scores = {'张三':100,'李四':98,'王五':45,}
print(scores)
print(type(scores))
#空字典
d = {}
print(d)
(2)使用内置函数dict():
#使用dict()
student = dict(name='jack',age=20)
print(student)
3.2 字典的删除
(1)del语句
对于字典中不再需要的信息,可使用del语句将相应的键-值对彻底删除,使用del语句时,必须指定字典名和要删除的键。删除的键-值对会永远消失。
scores = {'张三':100,'李四':98,'王五':45}
del scores['张三']
print(scores) #运行结果:{'李四': 98, '王五': 45}
(2)pop( )方法
pop( )方法删除指定键-值对的键。
scores = {'张三':100,'李四':98,'王五':45}
item = scores.pop('张三')
print(item) #运行结果:100
print(scores)#运行结果:{'李四': 98, '王五': 45}
(3)popitem( )方法
popitem()方法删除最后一个键-值对。
scores = {'张三':100,'李四':98,'王五':45}
item = scores.popitem()
print(item) #运行结果:('王五', 45)
print(scores)#运行结果:{'张三': 100, '李四': 98}
(4)clear( )方法
clear方法清空字典内容。
scores = {'张三':100,'李四':98,'王五':45}
item = scores.clear()
print(item) #运行结果:None
print(scores)#运行结果:{}
四、字典的查询操作
4.1 字典中元素的获取
(1)[ ] → 举例:scores['张三']
(2)get()方法→举例:scores.get('张三')
4.2 [ ]取值与使用get()取值的区别
(1)[ ]如果字典中不存在指定的key,抛出keyError异常
(2)get ()方法取值,如果字典中不存在指定的key,并不会抛出keyerror而是返回none,可以通过参数设置默认的value,以便指定的key不存在时返回
五、字典元素的增、删、改操作
5.1 key的判断
#key的判断
scores = {'张三':100,'李四':98,'王五':45}
print('张三' in scores) 运行结果:True
print('张三' not in scores) 运行结果:False
5.2 字典元素的删除
scores = {'张三':100,'李四':98,'王五':45}
# del scores ['张三'] #删除指定的键值对
# print(scores)
scores.clear( #清空字典的元素
print(scores)
5.3 字典元素的新增
scores = {'张三':100,'李四':98,'王五':45}
scores['陈六'] = 98 #新增元素
print(scores)
scores['陈六'] = 100 #修改元素
print(scores)
5.4 获取字典视图的三个方法
(1)keys():获取字典中所有key
(2)values():获取字典中所有value
(3)items():获取字典中所有key-value对
scores = {'张三':100,'李四':98,'王五':45}
#获取所有的key
keys = scores.keys()
print(keys)
print(type(keys))
print(list(keys)) #将所有键组成的视图转成列表
#获取所有的value
values = scores.values()
print(values)
print(type(values))
print(list(values))
#获取所有键值对
items = scores.items()
print(items)
print(list(items)) #转换之后的列表元素是由元组组成(元组在下章讲)
5.5 字典元素的遍历
scores = {'张三':100,'李四':98,'王五':45}
#字典元素的遍历
for item in scores:
print(item,scores[item],scores.get(item))
运行结果:
张三 100 100
李四 98 98
王五 45 45
5.6 字典的特点
(1)字典中的所有元素都是一个key-value对,key不允许重复,value可以重复
d = {'name':'bob','name':'jerry'} #key不允许重复
print(d) 运行结果:{'name': 'jerry'}
d = {'name':'bob','nikename':'bob'} #value可以重复
print(d) 运行结果:{'name': 'bob', 'nikename': 'bob'}
(2)字典中的元素是无序的
(3)字典中的key必须是不可变对象
(4)字典可以根据需要动态地伸缩
(5)字典会浪费较大的内存,是一种使用空间换时间的数据结构
六、字典生成式
6.1 内置函数zip( )
items = ['Fruits','Books','Others']
prices = [96,78,85]
d = {item:price for item,price in zip(items,prices)}
print(d) 运行结果:{'Fruits': 96, 'Books': 78, 'Others': 85}
items = ['Fruits','Books','Others']
prices = [96,78,85]
d = {item.upper():price for item,price in zip(items,prices)}
print(d) 运行结果:{'FRUITS': 96, 'BOOKS': 78, 'OTHERS': 85}
items = ['Fruits','Books','Others']
prices = [96,78,85,90]
d = {item.upper():price for item,price in zip(items,prices)}
print(d) 运行结果:{'FRUITS': 96, 'BOOKS': 78, 'OTHERS': 85}
未完待续.................................................................
原创不易,各位看官请随手点下Follow和Star,感谢!!!