[英]Right way to loop through list of dictionary with a dictionary in Python
I have an list of vehicles and a dict of filters. 我有车辆列表和过滤器字典。 The code is as follows:
代码如下:
The dictionary of filters is as follows: 过滤词典如下:
filters = {
'make': 'SKODA',
'model': 'Fabia',
'fuel': 'Diesel',
'transmission': '',
'registration_year': '',
'price': {'start': 10000, 'stop': 12000}
}
And the list of vehicles is as follows: 车辆清单如下:
vehicles = [
{'make': 'AUDI', 'model': 'Q2 Dsl', 'type': '1.6 TDi Sport', 'fuel': 'Diesel', 'mileage': '19896', 'registration_year': '2017', 'transmission': 'Handbediende versnellingsbak', 'price': 17800},
{'make': 'AUDI', 'model': 'A6 SW Dsl', 'type': '2.0 TDi S line', 'fuel': 'Diesel', 'mileage': '87354', 'registration_year': '2013', 'transmission': 'Handbediende versnellingsbak', 'price': 52000},
{'make': 'SKODA', 'model': 'Fabia', 'type': '1.6 CR TDi GreenLine Active DPF', 'fuel': 'Diesel', 'mileage': '90613', 'registration_year': '2012', 'transmission': 'Handbediende versnellingsbak', 'price': 11000},
{'make': 'AUDI', 'model': 'A4 SW Dsl', 'type': '2.0 TDi S tronic', 'fuel': 'Diesel', 'mileage': '47402', 'registration_year': '2016', 'transmission': 'Sequentiele bak', 'price': 93000},
{'make': 'VOLKSWAGEN', 'model': 'Touran', 'type': '1.4 TSI Trendline', 'fuel': 'Essence', 'mileage': '28588', 'registration_year': '2017', 'transmission': 'Handbediende versnellingsbak', 'price': 87000},
{'make': 'AUDI', 'model': 'A4 Dsl', 'type': '2.0 TDi', 'fuel': 'Diesel', 'mileage': '66053', 'registration_year': '2014', 'transmission': 'Handbediende versnellingsbak', 'price': 62000}
]
Thus, I want to loop through all vehicles and all filters and get back only those vehicles that match the filters. 因此,我想循环遍历所有车辆和所有过滤器并仅返回与过滤器匹配的车辆。 In this case I should get only Skoda .
在这种情况下,我应该只得到斯柯达 。
I tried with pydash
library, but without success. 我试过
pydash
库,但没有成功。
What have I tried is as follows: 我尝试了什么如下:
def filter_item(vehicle):
vehicles = []
for key, value in filters.items():
if key == "price":
if vehicle[key] > value['start'] and vehicle[key] < value['stop']:
vehicles.append(vehicle)
else:
if vehicle[key] == value:
vehicles.append(vehicle)
return vehicles
result = py_.filter(vehicles, lambda x: filter_item(x))
But in this case I get all vehicles with fuel type Diesel , but I should get only the second one if we start from 0, thus only Skoda. 但是在这种情况下,我得到所有带有燃料类型柴油的车辆,但如果我们从0开始,我应该只获得第二辆,因此只有斯柯达。
Any idea how can I do that? 知道我该怎么办? And is there a better way or cleaner code to do it?
还有更好的方法或更清洁的代码吗?
you need to check agaist all filter conditions, here is an example with python builtin filter
: 你需要检查agaist所有过滤条件,这里有一个python内置
filter
的例子:
def filter_item(vehicle):
vehicles = []
for key, value in filters.items():
if not value: # skip empty filter
continue
if key == "price":
if vehicle[key] > value['start'] and vehicle[key] < value['stop']:
continue
else:
if vehicle[key] == value:
continue
return False # no filter matched
return True
result = filter(filter_item, vehicles)
with pydash: 与pydash:
result = py_.filter(vehicles, filter_item)
note: callback function passed to fitler
returns true, to indicates the current value should include in the result. 注意:传递给
fitler
回调函数返回true,表示当前值应包含在结果中。
I think the price range should be split off from filters
. 我认为价格范围应该从
filters
分离出来。 It's awkward in there because it's the only key that you can't directly match with the corresponding values for the same key of a vehicle. 它在那里很尴尬,因为它是唯一不能直接匹配车辆相同键的相应值的键。
I'd write it like this 我会这样写的
>>> filters = {
...: 'make': 'SKODA',
...: 'model': 'Fabia',
...: 'fuel': 'Diesel',
...: 'transmission': '',
...: 'registration_year': '',
...:}
>>> price_range = range(10000, 12000 + 1)
>>>
>>> [vehicle['make'] for vehicle in vehicles if
...:all(vehicle.get(k) == v for k, v in filters.items() if v) and
...:vehicle['price'] in price_range]
>>> ['SKODA']
or like this 或者像这样
>>> [vehicle for vehicle in vehicles if
...:all(vehicle.get(k) == v for k, v in filters.items() if v) and
...:vehicle['price'] in price_range]
>>>
[{'fuel': 'Diesel',
'make': 'SKODA',
'mileage': '90613',
'model': 'Fabia',
'price': 11000,
'registration_year': '2012',
'transmission': 'Handbediende versnellingsbak',
'type': '1.6 CR TDi GreenLine Active DPF'}]
in case you want the full vehicle-dictionaries that match. 如果你想要匹配的完整车辆词典。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.