【发布时间】:2021-12-05 04:53:36
【问题描述】:
我有一本像这样的字典
my_dict = {'i': 5, 'citizens': 12, 'you': 344, 'the': 567, 'me': 11, 'states': 22, '': 104, 'in': 156}
还有一个看起来像的列表
ignore_words = ['i', 'we', 'you', 'he', 'she', 'it', 'they', 'me', 'us']
我想编辑字典以删除其中一个键与列表中的某个项目匹配的所有键值对,因此我的输出如下所示:
my_dict = {'citizens': 12, 'the': 567, 'states': 22, '': 104, 'in': 156}
我试过了
[my_dict.pop(key) for key in ignore_words]
但我得到了KeyError。
最好的方法是什么?
【问题讨论】:
标签: python list dictionary