8.基本数据类型dict (Dictionary)字典

本文介绍了Python中的字典数据类型,包括字典的定义、键值对的组成以及键的哈希算法。强调了key必须是可hash类型,如int、str、tuple和bool,而value类型可以任意。讨论了字典的增删改查操作,如查询、增加、修改和删除,并提到了get()、setdefault()、update()、pop()、popitem()和clear()等内置函数。还探讨了字典的嵌套使用,允许在字典中嵌套其他字典,只要key满足可hash条件。内容来源于VS code自带的例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

dict类型是字典,称键值对列表,用花括号{}括起来,元素用逗号隔开。
语法:{“key1”:value1,…}.
hash算法:根据key来计算出⼀个内存地址. 然后将key-value保存在这个地址中.
☑key必须是可hash类型,理解为不可改变,value类型随意。
可hash类型:int/str/tuple/bool;不可hash类型:list/dict/set(集合)。

car = {  'brand': 'Ford',
         'model': 'Mustang',
         'year': 1964
      }

上面是字典的定义,car字典包含了自己的键值对内容。


以下为:增删改查操作(内置函数)

?查询 -⽤key作为下标来查找对应的value值. / get()

dict1 = { "name": 'hassgy', "age": 20, "job": 'doctor'}
# 通过下标查询key的value,如果没有返回None
print(dict1['name'])
print(dict1['age'])
print(dict1['job'])
-------------------------------------------------------
car = {  'brand': 'Ford',  'model': 'Mustang',  'year': 1964}
# 返回一个key的value值,如果没有第二个参数,返回默认值
x = car.get('price', 15000)
print(x)

?增加 直接法 / setdefault()

car = {  'brand': 'Ford',  'model': 'Mustang',  'year': 1964}
# 如果car中没有这个‘price’key,直接新增该键值对。
car['price'] = 2650000
print(car)
# 第二种增加方法
x = car.setdefault('color', 'white')
print(x)

?修改 update()

car = {  'brand': 'Ford',  'model': 'Mustang',  'year': 1964}
car.update({'color': 'White'})
print(car)

?删除 pop() / popitem() / clear()

car = {  'brand': 'Ford',  'model': 'Mustang',  'year': 1964}
# 删掉最后一个(出栈)
car.pop('model')
print(car)
# 随机删除
car.popitem()
print(car)
# 清空car字典
car.clear()
print(car)

其他内置函数操作

# 构建新字典
x = dict(name = 'John', age = 36, country = 'Norway')
print(x)
#构建空值的键值对字典的方法
x = ('key1', 'key2', 'key3')
thisdict = dict.fromkeys(x)
print(thisdict)
--------------------------------------------------------------
{'key1': None, 'key2': None, 'key3': None}
--------------------------------------------------------------
# 浅拷贝(后续会详细讲,还有深拷贝)
car = {  'brand': 'Ford',  'model': 'Mustang',  'year': 1964}
x = car.copy()
print(x)
# 返回一个list列表,里面是tuple元组
x = car.items()
car['year'] = 2018
print(x)
----------------------------------------------------------------------
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 2018)])
----------------------------------------------------------------------
# 返回一个key列表list
x = car.keys()
car['color'] = 'white'
print(x)
#返回一个value列表list
x = car.values()
car['year'] = 2018
print(x)

字典之间嵌套使用

car = {  'brand': 'Ford',  
         'model': 'Mustang',  
         'year': 1964,  
         'car_class': {        
         		'brand': 'Hass',        
         		'model': 'huya',        
         		'year': 1245    
         		}'children': ['child1', 'child2'],
       	'desc': 'He does not like you!!!'
       }

字典里面又嵌套字典,只要key是可hash类型即可,value任何类型数据都可以。

print(car.get('year'))
# value是一个字典,接着用字典的函数进行操作
print(car.get('car_class').get('model'))
# value是一个列表,接着用列表函数进行操作
print(car.get(chidren)[1])

最后就是集合,下次更新~
代码素材采摘VS code自带例子。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值