字典还用于多种日常应用中,例如日间编程,Web开发和AI / ML编程,从而使其成为一个有用的容器。因此,了解实现与词典使用相关的不同任务的方法总是有好处的。

示例# using del

# Initializing dictionary

test_dict = {"Vishesh" : 29, "Ram" : 21, "Vishal" : 27, "Prashant" : 25}

# Printing dictionary before removal

print ("The dictionary before performing remove is : " + str(test_dict))

# Using del to remove a dict

del test_dict['Vishal']

# Printing dictionary after removal

print ("The dictionary after remove is : " + str(test_dict))

# using pop()# Initializing dictionary

test_dict = {"Vishesh" : 29, "Ram" : 21, "Vishal" : 27, "Prashant" : 25}

# Printing dictionary before removal

print ("The dictionary before performing remove is : " + str(test_dict))

# Using pop() to remove a dict. pair

removed_value = test_dict.pop('Ram')

# Printing dictionary after removal

print ("The dictionary after remove is : " + str(test_dict))

print ("The removed key's value is : " + str(removed_value))

# Using pop() to remove a dict. pair doesn't raise exception

# assigns 'No Key found' to removed_value

removed_value = test_dict.pop('Nilesh', 'No Key found')

# Printing dictionary after removal

print ("The dictionary after remove is : " + str(test_dict))

print ("The removed key's value is : " + str(removed_value))

# using items() + dict comprehension

# Initializing dictionary

test_dict = {"Vishesh" : 29, "Ram" : 21, "Vishal" : 27, "Prashant" : 25}

# Printing dictionary before removal

print ("The dictionary before performing remove is : " + str(test_dict))

# Using items() + dict comprehension to remove a dict. pair

new_dict = {key:val for key, val in test_dict.items() if key != 'Prashant}

# Printing dictionary after removal

print ("The dictionary after remove is : " + str(new_dict))

'

python按哪个键删除_Python-从字典中删除键的方法相关推荐

  1. python字典get计数_python字典中的get方法与setdefault方法

    Python的字典中的get()方法,接受一个键和一个缺省值.如果键在字典中,get返回对应的值,否则返回缺省值,并不会修改字典或添加不存在的键值对. dic = dict(zip('abc',ran ...

  2. python字典转换为列表_Python中将字典转换为列表的方法

    说明:列表不可以转换为字典 ①转换后的列表为无序列表 a = {'a' : 1, 'b': 2, 'c' : 3} #字典中的key转换为列表 key_value = list(a.keys()) p ...

  3. 如何从 Python 字典中删除键

    字典是 Python 中一个非常强大的数据集合.字典有助于使数据库操作更快. 你可以向一个现有的字典追加项目,也可以删除项目. 在这篇文章中,我们将学习如何使用两种方法来删除"键" ...

  4. Python字典中的setdefault方法

    文章目录 1. setdefault()方法 2. setdefault()方法构造value值为列表/字典的字典 2.1 构造value值为列表的字典 2.2 构造value值为字典的字典 3. s ...

  5. MySQL删除或清空表中数据的方法

    原标题:MySQL删除或清空表中数据的方法 要想把MySQL中的数据删除或清空,可以通过两种方法来实现:DELETE语句和TRUNCATE TABLE语句. DELETE语句可以将表中所有记录都删除, ...

  6. mysql删除表中所有数据_MySQL删除或清空表中数据的方法

    原标题:MySQL删除或清空表中数据的方法 要想把MySQL中的数据删除或清空,可以通过两种方法来实现:DELETE语句和TRUNCATE TABLE语句. DELETE语句可以将表中所有记录都删除, ...

  7. python中字典统计成绩合_Python统计字典中的项

    我想数一数语音标签.到目前为止,我已经将词性标记(德语)存储在字典中,其中POS标记的键和出现次数的值.在 当我数数的时候,我想把"NN"和"NE"总结为一个变 ...

  8. python统计字典里面value出现的次数_python统计字典中元素出现的次数

    有一个功能,想必大家经常能用到,那就是我们办公软件上常用的查找内容,因为有这个功能存在,所以我们在办公时候,效率特别的快,而语言的存在是要我们快速去得到结果,提高我们的效率,想必大家听到这里,就应该知 ...

  9. python如何读取字典的关键字_python提取字典key列表的方法

    python提取字典key列表的方法 更新时间:2015年07月11日 12:04:48 作者:企鹅不笨 这篇文章主要介绍了python提取字典key列表的方法,涉及Python中keys方法的使用技 ...

最新文章

  1. Facebook失误泄露反恐审查员信息 生命或受威胁
  2. Laravel之Eloquent ORM
  3. 15数码 java,15数码问题
  4. 组织模式 - Introduction
  5. 多人互砍游戏的后台服务器的多线程架构
  6. 详解Windows开机自动运行
  7. Oracle入门(三)之连接与登录
  8. 哪个html标签用于定义文档的标题,在HTML中,(41)用于定义文档的标题。
  9. 零基础入门Python3-列表list详解
  10. 微软为 Chromium 版 Edge 浏览器推出新的 logo
  11. 神经元与网络结构概论
  12. mysql.sock文件丢失的一个原因
  13. SqlBulkCopy(大数据量拷贝)
  14. jpg怎么合成一份_怎么将多个jpg合并成一个pdf
  15. 计算机二级保存错地方,盘点考计算机二级那些容易出错的地方
  16. php判断是否连续出现数字,php通过gbk编码判断 含有连续数字 可用于判断QQ号,手机号等。...
  17. python人脸特征提取_Python实现识别人脸特征并打印出来
  18. Kamiya丨Kamiya艾美捷大鼠微量白蛋白酶联免疫吸附试验说明书
  19. vue导出excel加一个进度条_使用vue导出excel文件
  20. python控制机器人走直线_python程序控制NAO机器人行走

热门文章

  1. 南宁计算机职业学校地址,南宁市第三职业技术学校
  2. python file tell_Python3 File tell() 方法
  3. 解决只能滑动弹框内容不能滑动弹框底层内容
  4. 微信小程序 自定义组件(stepper)
  5. 数据库授予用户增删改查的权限的语句_mysql数据库终端上的增删改查及权限等相关操作...
  6. 【方法】如何限定IP访问Oracle数据库
  7. MyCat:取代Cobar数据库中间件
  8. iOS 之 Property List
  9. android:#FFFFFFFF 颜色码解析
  10. js方法实现rgb颜色转换成16进制格式的代码的方法