[英]Issue when trying to filter dictionary list using list comprehension in Python
我想从字典列表中过滤,例如对应项目下方的字典列表,其中fileName
是objectAttribute
值中的tree
,并且没有以下值: green-field
, snowy-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-field
, snowy-field
或yellow-field
是highway_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))
这不是最优雅的,并且可能有一种更快的方法来做到这一点,而无需循环列表两次。
但简单地说:
Output: 'highway_bost183'
这样做让我思考。 只拥有一个字典,以文件名作为键,然后以属性列表作为值不是更好吗? 这将使这个过程更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.