ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python学习之字典常用方法

2022-02-06 16:03:21  阅读:222  来源: 互联网

标签:常用 花和尚 键名 Python items print heroes 字典


目录

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 = heroes.copy()
print(heroes_copy)

运行结果: 

 

标签:常用,花和尚,键名,Python,items,print,heroes,字典
来源: https://blog.csdn.net/weixin_56197703/article/details/122770442

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有