使用Python中的字典循环浏览字典列表的正确方法
我有一份车辆清单和一份过滤器清单。代码如下:使用Python中的字典循环浏览字典列表的正确方法,python,Python,我有一份车辆清单和一份过滤器清单。代码如下: filters = { 'make': 'SKODA', 'model': 'Fabia', 'fuel': 'Diesel', 'transmission': '', 'registration_year': '', 'price': {'start': 10000, 'stop': 12000} } vehicles = [ {'make': 'AUDI', 'model': 'Q2 Dsl', 'type': '1
filters = {
'make': 'SKODA',
'model': 'Fabia',
'fuel': 'Diesel',
'transmission': '',
'registration_year': '',
'price': {'start': 10000, 'stop': 12000}
}
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}
]
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))
过滤器字典如下所示:
filters = {
'make': 'SKODA',
'model': 'Fabia',
'fuel': 'Diesel',
'transmission': '',
'registration_year': '',
'price': {'start': 10000, 'stop': 12000}
}
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}
]
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))
车辆清单如下:
filters = {
'make': 'SKODA',
'model': 'Fabia',
'fuel': 'Diesel',
'transmission': '',
'registration_year': '',
'price': {'start': 10000, 'stop': 12000}
}
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}
]
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))
因此,我希望通过所有车辆和所有过滤器进行循环,并仅返回与过滤器匹配的车辆。在这种情况下,我应该只购买斯柯达
我尝试了pydash
library,但没有成功
我试过的方法如下:
filters = {
'make': 'SKODA',
'model': 'Fabia',
'fuel': 'Diesel',
'transmission': '',
'registration_year': '',
'price': {'start': 10000, 'stop': 12000}
}
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}
]
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))
def过滤器项目(车辆):
车辆=[]
对于键,filters.items()中的值:
如果键==“价格”:
如果车辆[钥匙]>值['start']和车辆[钥匙]<值['stop']:
车辆。附加(车辆)
其他:
如果车辆[钥匙]==值:
车辆。附加(车辆)
返回车辆
结果=过滤器(车辆,λx:过滤器项目(x))
但在这种情况下,我得到的是所有燃油类型为柴油的车辆,但如果从0开始,我应该只得到第二种,因此只有斯柯达。
知道我该怎么做吗?还有更好的方法或更干净的代码吗 您需要检查所有过滤器条件,下面是一个python内置过滤器的示例:
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)
注意:传递给fitler的回调函数返回true,表示当前值应该包含在结果中。我认为价格范围应该从
过滤器中分离出来。这很尴尬,因为它是唯一一个不能直接与车辆同一钥匙对应值匹配的钥匙
我会这样写
>>> 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']
>>> [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'}]
还是像这样
>>> 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']
>>> [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'}]
以防您需要匹配的整车词典