繁体   English   中英

尝试使用 Python 中的列表理解过滤字典列表时出现问题

[英]Issue when trying to filter dictionary list using list comprehension in Python

我想从字典列表中过滤,例如对应项目下方的字典列表,其中fileNameobjectAttribute值中的tree ,并且没有以下值: green-fieldsnowy-fieldyellow-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)
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 以获取值

Output: 'highway_bost183'

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM