【问题标题】:Issue when trying to filter dictionary list using list comprehension in Python尝试在 Python 中使用列表理解过滤字典列表时出现问题
【发布时间】:2021-03-26 03:27:38
【问题描述】:

我想从字典列表中过滤,例如在 fileNametreeobjectAttribute 值中的对应项目下方的列表,并且没有以下值:green-fieldsnowy-field也不是yellow-field

array = [
    {'fileName': 'waterfall_074', 'objectAttribute': 'black-cliff'},
    {'fileName': 'waterfall_074', 'objectAttribute': 'waterfall'}, 
    {'fileName': 'waterfall_074', 'objectAttribute': 'black-cliff'}, 
    {'fileName': 'opencountry_test_010', 'objectAttribute': 'red-flower'}, 
    {'fileName': 'opencountry_test_010', 'objectAttribute': 'overcast-sky'}, 
    {'fileName': 'highway_bost183', 'objectAttribute': 'cloudy-sky'}, 
    {'fileName': 'highway_bost183', 'objectAttribute': 'tree'},
    {'fileName': 'highway_bost183', 'objectAttribute': 'tree'},
    {'fileName': 'highway_bost183', 'objectAttribute': 'road'},
    {'fileName': 'opencountry_076', 'objectAttribute': 'cloudy-sky'}, 
    {'fileName': 'opencountry_076', 'objectAttribute': 'yellow-field'}, 
    {'fileName': 'opencountry_092', 'objectAttribute': 'overcast-sky'}, 
    {'fileName': 'opencountry_092', 'objectAttribute': 'tree'},
    {'fileName': 'opencountry_092', 'objectAttribute': 'yellow-field'}, 
    {'fileName': 'opencountry_092', 'objectAttribute': 'green-field'},
    {'fileName': 'mountain_086', 'objectAttribute': 'dusthaze-sky'},
    {'fileName': 'mountain_086', 'objectAttribute': 'rocky-mountain'},
    {'fileName': 'ibis_001', 'objectAttribute': 'black-ibis'},
    {'fileName': 'ibis_001', 'objectAttribute': 'green-field'},
    {'fileName': 'ibis_001', 'objectAttribute': 'green-field'},
    {'fileName': 'bison08', 'objectAttribute': 'tree'},
    {'fileName': 'bison08', 'objectAttribute': 'black-bison'},
    {'fileName': 'bison08', 'objectAttribute': 'green-field'},
    {'fileName': 'volcano_0191', 'objectAttribute': 'dusthaze-sky'},
    {'fileName': 'volcano_0191', 'objectAttribute': 'rocky-mountain'}, 
    {'fileName': 'horse_097', 'objectAttribute': 'tree'},
    {'fileName': 'horse_097', 'objectAttribute': 'white-horse'},
    {'fileName': 'horse_097', 'objectAttribute': 'green-field'}
]

从上面的这个列表中,还有这个其他的项目列表,有 tree 作为 objectAttribute ['opencountry_092', 'horse_097', 'highway_bost183', 'bison08'] 并且您可以检查此列表中唯一没有任何这些值的值:green-fieldsnowy-fieldyellow-fieldhighway_bost183

我想出了以下代码,但是它不起作用

def busca_images(array):
  print(array)
  arrayFiltered = [n for n in array if 'tree' in n['objectAttribute'] ]
  newSet = set()
  for e in arrayFiltered:
    newSet.add(e['fileName'])
    files = []
  for e in newSet:
    if len([n for n in array if 'green-field' in n['objectAttribute'] or 'snowy-field' in n['objectAttribute'] or 'yellow-field' in n['objectAttribute'] ]) != 0: files.append(e)
  print(list(files))

我相信错误就在这个条件中......

if len([n for n in array if 'green-field' in n['objectAttribute'] or 'snowy-field' in n['objectAttribute'] or 'yellow-field' in n['objectAttribute'] ]) != 0: files.append(e)

【问题讨论】:

  • 如果您可以展示一个简短的、可重现的示例,其中包含您的输入、预期输出和当前获得的输出,将会很有帮助。
  • 好的,@BrendanAbel... 输入是我上面提到的那个,而我使用错误代码得到的输出与使用tree 过滤器得到的列表相同:@987654337 @ 并且预期的输出是'highway_bost183'
  • 真的不清楚你想在这里做什么
  • @jbflow 我只想指出哪些图像具有tree 但没有任何类型的字段(green-fieldsnowy-fieldyellow-field)。
  • "objectAttribute 值中fileName 为树的对应项" 抱歉,我无法理解。

标签: python filter set list-comprehension


【解决方案1】:
def busca_images(array):
    has_tree = set()
    has_field = set()
    final = set()
    for each in array:
        if each['objectAttribute'] == 'tree':
            has_tree.add(each['fileName'])
    for each in array:
        if each['fileName'] in has_tree and each['objectAttribute'] in ['green-field', 'yellow-field', 'snowy-field']:
            has_field.add(each['fileName'])
    return list(has_tree - has_field)[0]
print(busca_images(array))

这不是最优雅的,并且可能有一种更快的方法来做到这一点,而无需循环两次列表。

但很简单:

  • 循环列表以查找树,添加到 has_tree 集
  • 再次循环查找字段,添加到 has_fields 集
  • 从has_tree中减去has_field得到值

输出:'highway_bost183'

这样做让我思考。只拥有一个字典,以文件名作为键,然后以属性列表作为值不是更好吗?这将使这个过程变得容易得多。

【讨论】:

  • 谢谢@jbflow,但这并不是我想要达到的结果...'highway_bost183',因为这是正确答案
  • 但是满足您上面提到的标准的其他条目呢? {'fileName': 'bison08', 'objectAttribute': 'tree'} 有树没有字段?
  • @lucasbbs 我已经对其进行了编辑以给出该响应,方法是将您未提及的那些文件名添加到条件中。它现在返回highway_boost1283
  • OP 不想为 green-field 等 AFAICT 测试 fileName
  • @lucasbbs 好的,是的,我没有完全理解这个问题。我已经编辑了答案,现在它可以满足您的要求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-23
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 2018-03-18
  • 1970-01-01
  • 2013-01-21
相关资源
最近更新 更多