python - Pandas:如何在 python 字典中搜索和删除键值对以匹配模式
问题描述
我有一本下面列出的字典。
["key1" : "//This is an entry to be deleted", "key2" : "Good key", "key3", "Good key"]
对于所有以“//”开头的值都是坏键,因此必须删除它们。
如果有人没有得到我的问题,请告诉我。
解决方案
通过过滤使用字典理解startswith
:
d = {"key1" : "//This is an entry to be deleted", "key2" : "Good key", "key3": "Good key"}
d = {k:v for k, v in d.items() if not v.startswith('//')}
print (d)
{'key2': 'Good key', 'key3': 'Good key'}
通过过滤使用字典理解startswith
:
d = {"//key1" : "This is an entry to be deleted", "key2" : "Good key", "key3": "Good key"}
d = {k: v for k, v in dc.items() if not k.startswith('//')}
print (d)
{'key2': 'Good key', 'key3': 'Good key'}
推荐阅读
- python - Python Tkinter 按钮出现在第一个窗口
- python - 如何选择数据列表是 Python sqlite3 中所有列的子集的位置
- cordova - Android 上的谷歌地图显示为空白
- c++ - 在 C++ 中调整向量中的索引
- scala - 在 EMR 中使用 spark ad scala 从 redshift 加载数据
- python - 在 pandas 的行操作中需要帮助
- python - 使用 BeautifulSoup 返回被双标签包围的文本
- python - Pandas 在使用 loc 创建新列时设置 dtype
- spring-mvc - Spring restTemplate 没有返回任何东西
- android - 根据背景以编程方式更改波纹颜色