Python - 如果key等于value,则从列表中删除字典

时间:2017-02-15 21:57:57

标签: python dictionary

如何从列表中删除链接值包含在列表b中的所有词典?

while

应该是:

a = [{'link':'http://example.com/1/', 'id': 1}, {'link':'http://example.com/2/', 'id': 2}]
b = ['http://example.com/2/', 'http://example.com/3/']

1 个答案:

答案 0 :(得分:5)

a = [x for x in a if x['link'] not in b]

演示:

>>> a = [{'link':'http://example.com/1/', 'id': 1}, {'link':'http://example.com/2/', 'id': 2}]
>>> b = ['http://example.com/2/', 'http://example.com/3/']
>>> a = [x for x in a if x['link'] not in b]
>>> a
[{'link': 'http://example.com/1/', 'id': 1}]
相关问题