2017-02-15 97 views
3

如何从列表中删除链接列表b中包含链接值的所有字典?Python - 从列表中删除字典,如果键值等于

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 = [{'link':'http://example.com/1/', 'id': 1}] 

回答

3
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}] 
相关问题