如何从列表中删除链接值包含在列表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/']
答案 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}]