Python学习之字典常用方法

阅读: 评论:0

Python学习之字典常用方法

Python学习之字典常用方法

目录

1、增加:直接通过键名增加即可

 2、修改:

(1)直接修改:

(2)方法update():新建一个字典,通过方法update()将新的字典加入到原来的字典中。

 3、删除:

(1)del语句:根据键名删除一个元素或者整个字典。(不能通过索引进行删除)

(2)方法pop():根据键名进行删除。

(3)方法popitem():删除最后一个元素。

(4)方法clear():清空字典的元素。

 4、查询:in:根据键名进行查询。

5、取值:

(1)方法keys():取key(键名)。得到的是一个元组。也可以用for循环来依次遍历。

(2)方法values():取value(键值)。得到的是一个元组。也可以用for循环来依次遍历。

(3)方法items():获取字典中每个元素。得到的是一个元组。也可以用for循环来依次遍历。得到的依然是一个元组。

 6、复制:方法copy():复制一个新字典。


1、增加:直接通过键名增加即可

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
heroes['豹子头'] = '林冲'
print(heroes)

运行结果:

 2、修改:

(1)直接修改:

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
heroes['及时雨'] = '宋公明'
print(heroes)

 运行结果:

 

(2)方法update():新建一个字典,通过方法update()将新的字典加入到原来的字典中。

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
dict_val = {'黑旋风':'李逵','入云龙':'公孙胜'}
heroes.update(dict_val)
print(heroes)

运行结果:

注意:值相同,若新增的字典中的键名包含原来字典的键名,则最终的结果不会添加进来(因为键名只能有一个,没有重复)。例如: 

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
dict_val = {'黑旋风':'李逵','入云龙':'公孙胜','花和尚':'鲁智深'}
heroes.update(dict_val)
print(heroes)

 运行结果:

值不同,在若新增的字典中的键名包含原来字典的键名,则原字典键名对应的值会被新字典键名对应的值代替。例如: 

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
dict_val = {'黑旋风':'李逵','入云龙':'公孙胜','花和尚':'鲁达'}
heroes.update(dict_val)
print(heroes)

 运行结果:

 3、删除:

(1)del语句:根据键名删除一个元素或者整个字典。(不能通过索引进行删除)

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
del heroes['及时雨']
print(heroes)
del heroes
print(heroes)

运行结果:

(2)方法pop():根据键名进行删除。

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
heroes.pop('花和尚')
print(heroes)

 运行结果:

(3)方法popitem():删除最后一个元素。

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
heroes.popitem()
print(heroes)

运行结果:

(4)方法clear():清空字典的元素。

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
heroes.clear()
print(heroes)

运行结果: 

 4、查询:in:根据键名进行查询。

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
if '及时雨' in heroes:print('该元素在字典中')
else:print('该元素不在字典中')

运行结果:

5、取值:

(1)方法keys():取key(键名)。得到的是一个元组。也可以用for循环来依次遍历。

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
keys = heroes.keys()
print(keys)
for key in keys:print(key)

运行结果:  

注意:不能通过索引来获取每个键名,否则会报错。可将字典转换成列表或者元组再通过索引来获取每个键名。

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
keys = heroes.keys()
keys_list = list(keys)
print(keys_list[0])
keys_tuple = tuple(keys)
print(keys_tuple[0])

 运行结果:

(2)方法values():取value(键值)。得到的是一个元组。也可以用for循环来依次遍历。

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
values = heroes.values()
print(values)
for value in values:print(value)

 注意:不能通过索引来获取每个键值,否则会报错。可将字典转换成列表或者元组再通过索引来获取每个键值。

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
values = heroes.values()
values_list = list(values)
print(values_list[0])
values_tuple = tuple(values)
print(values_tuple[0])

 运行结果:

(3)方法items():获取字典中每个元素。得到的是一个元组。也可以用for循环来依次遍历。得到的依然是一个元组。

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
items = heroes.items()
print(items)
for item in items:print(item)

运行结果:

 也可以通过方法items()获取key-values(键值对)。

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
items = heroes.items()
print(items)
for key,value in items:print(f'{key}->{value}')

 运行结果:

 注意:不能通过索引来获取每个键值对,否则会报错。可将字典转换成列表或者元组再通过索引来获取每个键值对。同时通过元组拆包方式也可以获取键名和键值。

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
items = heroes.items()
items_list = list(items)
print(items_list[0])
items_tuple = tuple(items)
print(items_tuple[0])
key,value = items_list[0]
print(key,value)
key,value = items_tuple[0]
print(key,value)

 运行结果:

 6、复制:方法copy():复制一个新字典。

heroes = {'及时雨':'宋江','玉麒麟':'卢俊义','花和尚':'鲁智深','母夜叉':'孙二娘'}
heroes_copy = py()
print(heroes_copy)

运行结果: 

 

本文发布于:2024-01-30 18:18:40,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170660992221916.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字典   常用   方法   Python
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23