【问题标题】:Python - Remove dictionary from list if key is equal to value [duplicate]Python - 如果键等于值,则从列表中删除字典[重复]
【发布时间】:2017-07-04 18:58:12
【问题描述】:

如何从列表 a 中删除列表 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 应该是:

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

【问题讨论】:

    标签: python dictionary


    【解决方案1】:
    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}]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多