在 dictionaries/nested 词典列表 python 中搜索和过滤
Searching and Filtering in a list of dictionaries/nested dictionary python
您好,我在过滤此词典列表时遇到问题。我试图到达的是 all_assets 列表中的 'attributes' 字典并按(例如)背景:星号的值对其进行过滤。我目前的尝试是使用 filter() 和 lambda 函数。
我目前的情况是这样的:
all_assets = [{'dateListed': 58391,
'id': '118572',
'metadata': {'files': [],
'mediaType': 'image/png',
'name': 'The three',
'tags': [{'right': '101 Galaxy'},
{'attributes': {'Background': 'Mars',
'Body': 'Rough',
'Face': 'Dumb',
'Headwear': 'Helmet'}}],
'thumbnail': 'something'},
'Session': None,
'police': 'pewpew',
'verified': {'project': 'Thes', 'verified': True}},
{'dateListed': 430298239,
'id': '1191281',
'metadata': {'files': [],
'mediaType': 'image/png',
'name': 'TheOne',
'tags': [{'right': '101 Galaxy'},
{'attributes': {'Background': 'Star',
'Body': 'Smooth',
'Face': 'Fresh',
'Headwear': 'Cap'}}],
'thumbnail': 'something'},
'Session': None,
'police': 'pewpew',
'verified': {'project': 'Thes', 'verified': True}}]
search_attribute = 'Background'
search_attribute_value = 'Star'
filtered = filter(lambda i: any(d.get(search_attribute, 'xxx') == search_attribute_value for d in i['metadata']['tags']['attributes']), all_assets)
# print(list(filtered))
输出应该是因为搜索属性是星:
{'dateListed': 430298239,
'id': '1191281',
'metadata': {'files': [],
'mediaType': 'image/png',
'name': 'TheOne',
'tags': [{'right': '101 Galaxy'},
{'attributes': {'Background': 'Star',
'Body': 'Smooth',
'Face': 'Fresh',
'Headwear': 'Cap'}}],
'thumbnail': 'something'},
'Session': None,
'police': 'pewpew',
'verified': {'project': 'Thes', 'verified': True}}]
然后我想对过滤后的列表做的是打印出一些值
像 'id'、'name' 和 'headwear'.
for x in filtered:
id = (x.get('id'))
name = (x.get('metadata')('name'))
headwear = (x.get('metadata')('tags')('attributes')('Headwear'))
print(f'ID: {id}')
print(f'Name: {name}')
print(f'Headwear: {headwear}')
我正在尝试获取以下输出:
ID: 1191281
Name: TheOne
Headwear: Cap
我对 python 这个领域有些陌生,因为我对 lambda 函数只知道一点。很抱歉,如果这是一个愚蠢的问题。
我编写了自定义过滤方法:
def customFilter(all_assets, search_attribute, search_attribute_value):
res = []
for asset in all_assets:
if asset:
attributes = asset["metadata"]["tags"][1]["attributes"]
if attributes and search_attribute in attributes:
if search_attribute_value == attributes[search_attribute]:
res.append(asset)
return res
让我们测试一下:
all_assets = [
{
"dateListed": 58391,
"id": "118572",
"metadata": {
"files": [],
"mediaType": "image/png",
"name": "The three",
"tags": [
{"right": "101 Galaxy"},
{
"attributes": {
"Background": "Mars",
"Body": "Rough",
"Face": "Dumb",
"Headwear": "Helmet",
}
},
],
"thumbnail": "something",
},
"Session": None,
"police": "pewpew",
"verified": {"project": "Thes", "verified": True},
},
{
"dateListed": 430298239,
"id": "1191281",
"metadata": {
"files": [],
"mediaType": "image/png",
"name": "TheOne",
"tags": [
{"right": "101 Galaxy"},
{
"attributes": {
"Background": "Star",
"Body": "Smooth",
"Face": "Fresh",
"Headwear": "Cap",
}
},
],
"thumbnail": "something",
},
"Session": None,
"police": "pewpew",
"verified": {"project": "Thes", "verified": True},
},
]
search_attribute = "Background"
search_attribute_value = "Star"
print(customFilter(all_assets, search_attribute, search_attribute_value))
输出:
[{'dateListed': 430298239, 'id': '1191281', 'metadata': {'files': [], 'mediaType': 'image/png', 'name': 'TheOne', 'tags': [{'right': '101 Galaxy'}, {'attributes': {'Background': 'Star', 'Body': 'Smooth', 'Face': 'Fresh', 'Headwear': 'Cap'}}], 'thumbnail': 'something'}, 'Session': None, 'police': 'pewpew', 'verified': {'project': 'Thes', 'verified': True}}]
您好,我在过滤此词典列表时遇到问题。我试图到达的是 all_assets 列表中的 'attributes' 字典并按(例如)背景:星号的值对其进行过滤。我目前的尝试是使用 filter() 和 lambda 函数。
我目前的情况是这样的:
all_assets = [{'dateListed': 58391,
'id': '118572',
'metadata': {'files': [],
'mediaType': 'image/png',
'name': 'The three',
'tags': [{'right': '101 Galaxy'},
{'attributes': {'Background': 'Mars',
'Body': 'Rough',
'Face': 'Dumb',
'Headwear': 'Helmet'}}],
'thumbnail': 'something'},
'Session': None,
'police': 'pewpew',
'verified': {'project': 'Thes', 'verified': True}},
{'dateListed': 430298239,
'id': '1191281',
'metadata': {'files': [],
'mediaType': 'image/png',
'name': 'TheOne',
'tags': [{'right': '101 Galaxy'},
{'attributes': {'Background': 'Star',
'Body': 'Smooth',
'Face': 'Fresh',
'Headwear': 'Cap'}}],
'thumbnail': 'something'},
'Session': None,
'police': 'pewpew',
'verified': {'project': 'Thes', 'verified': True}}]
search_attribute = 'Background'
search_attribute_value = 'Star'
filtered = filter(lambda i: any(d.get(search_attribute, 'xxx') == search_attribute_value for d in i['metadata']['tags']['attributes']), all_assets)
# print(list(filtered))
输出应该是因为搜索属性是星:
{'dateListed': 430298239,
'id': '1191281',
'metadata': {'files': [],
'mediaType': 'image/png',
'name': 'TheOne',
'tags': [{'right': '101 Galaxy'},
{'attributes': {'Background': 'Star',
'Body': 'Smooth',
'Face': 'Fresh',
'Headwear': 'Cap'}}],
'thumbnail': 'something'},
'Session': None,
'police': 'pewpew',
'verified': {'project': 'Thes', 'verified': True}}]
然后我想对过滤后的列表做的是打印出一些值 像 'id'、'name' 和 'headwear'.
for x in filtered:
id = (x.get('id'))
name = (x.get('metadata')('name'))
headwear = (x.get('metadata')('tags')('attributes')('Headwear'))
print(f'ID: {id}')
print(f'Name: {name}')
print(f'Headwear: {headwear}')
我正在尝试获取以下输出:
ID: 1191281
Name: TheOne
Headwear: Cap
我对 python 这个领域有些陌生,因为我对 lambda 函数只知道一点。很抱歉,如果这是一个愚蠢的问题。
我编写了自定义过滤方法:
def customFilter(all_assets, search_attribute, search_attribute_value):
res = []
for asset in all_assets:
if asset:
attributes = asset["metadata"]["tags"][1]["attributes"]
if attributes and search_attribute in attributes:
if search_attribute_value == attributes[search_attribute]:
res.append(asset)
return res
让我们测试一下:
all_assets = [
{
"dateListed": 58391,
"id": "118572",
"metadata": {
"files": [],
"mediaType": "image/png",
"name": "The three",
"tags": [
{"right": "101 Galaxy"},
{
"attributes": {
"Background": "Mars",
"Body": "Rough",
"Face": "Dumb",
"Headwear": "Helmet",
}
},
],
"thumbnail": "something",
},
"Session": None,
"police": "pewpew",
"verified": {"project": "Thes", "verified": True},
},
{
"dateListed": 430298239,
"id": "1191281",
"metadata": {
"files": [],
"mediaType": "image/png",
"name": "TheOne",
"tags": [
{"right": "101 Galaxy"},
{
"attributes": {
"Background": "Star",
"Body": "Smooth",
"Face": "Fresh",
"Headwear": "Cap",
}
},
],
"thumbnail": "something",
},
"Session": None,
"police": "pewpew",
"verified": {"project": "Thes", "verified": True},
},
]
search_attribute = "Background"
search_attribute_value = "Star"
print(customFilter(all_assets, search_attribute, search_attribute_value))
输出:
[{'dateListed': 430298239, 'id': '1191281', 'metadata': {'files': [], 'mediaType': 'image/png', 'name': 'TheOne', 'tags': [{'right': '101 Galaxy'}, {'attributes': {'Background': 'Star', 'Body': 'Smooth', 'Face': 'Fresh', 'Headwear': 'Cap'}}], 'thumbnail': 'something'}, 'Session': None, 'police': 'pewpew', 'verified': {'project': 'Thes', 'verified': True}}]