【发布时间】:2021-03-26 03:27:38
【问题描述】:
我想从字典列表中过滤,例如在 fileName 是 tree 在 objectAttribute 值中的对应项目下方的列表,并且没有以下值: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)
【问题讨论】:
-
如果您可以展示一个简短的、可重现的示例,其中包含您的输入、预期输出和当前获得的输出,将会很有帮助。
-
好的,@BrendanAbel... 输入是我上面提到的那个,而我使用错误代码得到的输出与使用
tree过滤器得到的列表相同:@987654337 @ 并且预期的输出是'highway_bost183' -
真的不清楚你想在这里做什么
-
@jbflow 我只想指出哪些图像具有
tree但没有任何类型的字段(green-field、snowy-field或yellow-field)。 -
"objectAttribute 值中fileName 为树的对应项" 抱歉,我无法理解。
标签: python filter set list-comprehension