python中字典的过滤列表



根据过滤条件筛选字典

records = [
{"category": "automobile", "type": "car", "model": "suv", "year": "2010"},
{"category": "automobile", "type": "car", "model": "all", "year": "2010"},
{"category": "automobile", "type": "car", "model": "sedan", "year": "2010"},
{"category": "automobile", "type": "car", "model": "hatchback", "year": "2010"},
]
filter_value = {
"model": ["suv"]
}
filter_record = [
{"category": "automobile", "type": "car", "model": "suv", "year": "2010"},
]

记录:Records是未过滤数据的列表filter_value:Filter_value为搜索条件filter_record:这是过滤后的最终数据,如果过滤值不存在于记录中,则需要检查是否有任何记录具有'all'作为值例如:-

records = [
{"category": "automobile", "type": "car", "model": "suv", "year": "2010"},
{"category": "automobile", "type": "car", "model": "all", "year": "2010"},
{"category": "automobile", "type": "car", "model": "sedan", "year": "2010"},
{"category": "automobile", "type": "car", "model": "hatchback", "year": "2010"},
]
filter_value = {
"model": ["truck"]
}
filter_record = [
{"category": "automobile", "type": "car", "model": "all", "year": "2010"},
]

在上面的例子中'truck'没有出现在任何记录中,所以我们需要取出model = 'all'

我的代码:

filter_value = {
"model": ["suv"]
}
filter_record = list(filter(lambda item: (item["model"] in filter_value.get("model") or item["model"] == "all"), records))
print(filter_record)

输出: -

[{'category': 'automobile', 'type': 'car', 'model': 'suv', 'year': '2010'}, {'category': 'automobile', 'type': 'car', 'model': 'all', 'year': '2010'}]

谁能帮我一下,我在这里错过了什么,我是新的python

期望的输出应该是:-

[{'category': 'automobile', 'type': 'car', 'model': 'suv', 'year': '2010'}]

更新:

新例子——

records = [
{"category": "automobile", "type": "car", "model": "suv", "year": "2019"},
{"category": "aircraft", "type": "fixed wing", "model": "F35", "year": "2019"},
{"category": "aircraft", "type": "rotary wing", "model": "blackhawk", "year": "2019"},
{"category": "automobile", "type": "car", "model": "hatchback", "year": "2010"},
{"category": "aircraft", "type": "fixed wing", "model": "ALL", "year": "2019"},
{"category": "aircraft", "type": "fixed wing", "model": "F35", "year": "ALL"},
{"category": "aircraft", "type": "rotary wing", "model": "blackhawk", "year": "2022"},
{"category": "aircraft", "type": "rotary wing", "model": "blackhawk", "year": "ALL"},
{"category": "aircraft", "type": "rotary wing", "model": "ALL", "year": "2022"},
{"category": "aircraft", "type": "airline", "model": "747", "year": "2019"},
{"category": "aircraft", "type": "airline", "model": "747", "year": "2022"},
{"category": "aircraft", "type": "airline", "model": "747", "year": "2030"},
{"category": "aircraft", "type": "helicopters", "model": "hingeless", "year": "2019"},
{"category": "aircraft", "type": "helicopters", "model": "teetering", "year": "2019"},
{"category": "aircraft", "type": "helicopters", "model": "ALL", "year": "ALL"}
]

filter_value:

filter_value= {
"category" : ["aircraft"],
"model": ["F35","blackhawk","suv","747","sedan"], 
"year": ["2019","2022","2025"]
}

预期输出:

filter_record = [
{"category": "aircraft", "type": "fixed wing", "model": "F35", "year": "2019"}, # search combination = "category" : ["aircraft"],  "model": ["F35"], "year": ["2019"]
{"category": "aircraft", "type": "fixed wing", "model": "F35", "year": "ALL"}, # search combination = "category" : ["aircraft"],  "model": ["F35"], "year": ["2022"], we have record with "caregory" = "aircraft" and "model" = "F35" but not year = 2022, at this time we need to check if we have record with "category" = "aircraft"  "model" = "F35" "year"= "ALL"
{"category": "aircraft", "type": "fixed wing", "model": "F35", "year": "ALL"}, # search combination = "category" : ["aircraft"],  "model": ["F35"], "year": ["2025"], we have record with "caregory" = "aircraft" and "model" = "F35" but not year = 2025, at this time we need to check if we have record with "category" = "aircraft"  "model" = "F35" "year"= "ALL"
{"category": "aircraft", "type": "rotary wing", "model": "blackhawk", "year": "2019"}, # search combination = "category" : ["aircraft"],  "model": ["blackhawk"], "year": ["2019"]
{"category": "aircraft", "type": "rotary wing", "model": "blackhawk", "year": "2022"},# search combination = "category" : ["aircraft"],  "model": ["blackhawk"], "year": ["2022"]
{"category": "aircraft", "type": "rotary wing", "model": "blackhawk", "year": "ALL"},# search combination = "category" : ["aircraft"],  "model": ["blackhawk"], "year": ["2025"], we have record with "caregory" = "aircraft" and "model" = "blackhawk" but not year = 2025, at this time we need to check if we have record with "category" = "aircraft"  "model" = "blackhawk" "year"= "ALL"
{"category": "aircraft", "type": "fixed wing", "model": "ALL", "year": "2019"},# search combination = "category" : ["aircraft"],  "model": ["suv"], "year": ["2019"], we have record with "caregory" = 'aircraft' and "year" ='2019' but not "model" = "suv", at this time we need to check if we have record with "category" ="aircraft",  "model" = "ALL", "year" = "2019"
{"category": "aircraft", "type": "rotary wing", "model": "ALL", "year": "2022"}, # search combination = "category" : ["aircraft"],  "model": ["suv"], "year": ["2022"], we have record with "caregory" = 'aircraft' and "year" ='2022' but not "model" = "suv", at this time we need to check if we have record with "category" ="aircraft",  "model" = "ALL", "year" = "2022"
{"category": "aircraft", "type": "helicopters", "model": "ALL", "year": "ALL"}, # search combination = "category" : ["aircraft"],  "model": ["suv"], "year": ["2025"], we have record with "caregory" = 'aircraft' but not "model" = "suv" nor and "year" ='2025', at this time we need to check if we have record with "category" ="aircraft",  "model" = "ALL", "year" = "ALL"
{"category": "aircraft", "type": "airline", "model": "747", "year": "2019"},# search combination = "category" : ["aircraft"],  "model": ["747"], "year": ["2019"]
{"category": "aircraft", "type": "airline", "model": "747", "year": "2022"}, # search combination = "category" : ["aircraft"],  "model": ["747"], "year": ["2022"]
{}, # search combination = "category" : ["aircraft"],  "model": ["747"], "year": ["2025"], we have record with "caregory" = 'aircraft' and model = "747" but we do not have year = '2025' or year = 'ALL'
{"category": "aircraft", "type": "fixed wing", "model": "ALL", "year": "2019"}, # search combination = "category" : ["aircraft"],  "model": ["sedan"], "year": ["2019"], we have record with "caregory" = 'aircraft' and "year" ='2019' but not "model" = "sedan", at this time we need to check if we have record with "category" ="aircraft",  "model" = "ALL", "year" = "2019"
{"category": "aircraft", "type": "rotary wing", "model": "ALL", "year": "2022"}, # search combination = "category" : ["aircraft"],  "model": ["sedan"], "year": ["2022"], we have record with "caregory" = 'aircraft' and "year" ='2022' but not "model" = "sedan", at this time we need to check if we have record with "category" ="aircraft",  "model" = "ALL", "year" = "2022"
{"category": "aircraft", "type": "helicopters", "model": "ALL", "year": "ALL"} # search combination = "category" : ["aircraft"],  "model": ["sedan"], "year": ["2025"], we have record with "caregory" = 'aircraft' but not "model" = "sedan" nor and "year" ='2025', at this time we need to check if we have record with "category" ="aircraft",  "model" = "ALL", "year" = "ALL"
]
records = [
{"category": "automobile", "type": "car", "model": "suv", "year": "2010"},
{"category": "automobile", "type": "car", "model": "all", "year": "2010"},
{"category": "automobile", "type": "car", "model": "sedan", "year": "2010"},
{"category": "automobile", "type": "car", "model": "hatchback", "year": "2010"},
]
filter_value = {
"model": ["suv", "sedan", "truck"]
}
out = []
for f in filter_value['model']:
filter_record = list(filter(lambda item: item["model"] == f, records))
if not filter_record:
filter_record = list(filter(lambda item: item["model"] == "all", records))
out.extend(filter_record)
print(out)
[{'category': 'automobile', 'type': 'car', 'model': 'suv', 'year': '2010'}, {'category': 'automobile', 'type': 'car', 'model': 'sedan', 'year': '2010'}, {'category': 'automobile', 'type': 'car', 'model': 'all', 'year': '2010'}]

如果您有多个搜索条件,这样的方法可能有效。它在计算上不太优雅,但是可以很好地控制要搜索的内容:

# Records is always a list of dictionaries. Every dictionary has exactly the same
# keys as the others. 
# Any record that has a key whose value is "all", will be returned for any search
# that includes that field. For instance, below, any search for any particular
# model will always return the second record - because whatever "model" you search
# for, "all" meets that description. 
records = [
{"category": "automobile", "type": "car", "model": "suv", "year": "2010"},
{"category": "automobile", "type": "car", "model": "all", "year": "2010"},
{"category": "automobile", "type": "car", "model": "sedan", "year": "2010"},
{"category": "automobile", "type": "car", "model": "hatchback", "year": "2010"},
]

# search_terms is a dictionary where every key must be one of the keys of one of the
# dictionaries comprising records above. Every value is a list of strings. 
search_terms = {
"model": ["suv", "sedan"]
}

def search_records(records, search_terms):
"""
Returns a list of dictionaries, each dictionary being a single record from 
"records" above.
"""
filter_record = [] # Initiate an empty list to hold the returned records.
# step through every key in "search_terms"
for search_field, terms in search_terms.items(): 
# within that key, step through every specific value searched for
for term in terms:
# search through every record in records
for record in records:
if all([record not in filter_record, (record[search_field] == term or record[search_field] == 'all')]):
filter_record.append(record)

return filter_record
filter_record = search_records(records, search_terms)
for record in filter_record:
print (record)

输出:

{'category': 'automobile', 'type': 'car', 'model': 'suv', 'year': '2010'}
{'category': 'automobile', 'type': 'car', 'model': 'all', 'year': '2010'}
{'category': 'automobile', 'type': 'car', 'model': 'sedan', 'year': '2010'}

要验证它适用于不同类型的记录和多个搜索条件,请将记录更改为:

records = [
{"category": "automobile", "type": "car", "model": "suv", "year": "2010"},
{"category": "aircraft", "type": "fixed wing", "model": "F35", "year": "2019"},
{"category": "aircraft", "type": "rotary wing", "model": "blackhawk", "year": "1999"},
{"category": "automobile", "type": "car", "model": "hatchback", "year": "2010"},
]

和你的搜索条件:

search_terms = {
"model": ["suv"],
"category" : ["aircraft"]
}

和调用相同的函数,你会得到这样的输出:

{'category': 'automobile', 'type': 'car', 'model': 'suv', 'year': '2010'}
{'category': 'aircraft', 'type': 'fixed wing', 'model': 'F35', 'year': '2019'}
{'category': 'aircraft', 'type': 'rotary wing', 'model': 'blackhawk', 'year': '1999'}

同样将搜索条件更改为

search_terms = {
"type" : ["fixed wing"], 
"model" : ["hatchback"]
}

给你:

{'category': 'aircraft', 'type': 'fixed wing', 'model': 'F35', 'year': '2019'}
{'category': 'automobile', 'type': 'car', 'model': 'hatchback', 'year': '2010'}

这能满足你的要求吗?

相关内容

  • 没有找到相关文章

最新更新