Python 如何从两个单独的列表中为每个键创建多个值的字典?
我正在尝试创建一个每个键都有多个值的字典。例如:Python 如何从两个单独的列表中为每个键创建多个值的字典?,python,list,loops,dictionary,Python,List,Loops,Dictionary,我正在尝试创建一个每个键都有多个值的字典。例如: top_10 = ['Volkswagen_Golf_1.4', 'BMW_316i', 'Ford_Fiesta', 'BMW_318i', 'Volkswagen_Polo', 'BMW_320i', 'Opel_Corsa', 'Renault_Twingo', 'Volkswagen_Golf', 'Opel_Corsa_1.2_16V'] common_brands = ['volkswagen', 'bmw', 'opel', 'm
top_10 = ['Volkswagen_Golf_1.4', 'BMW_316i', 'Ford_Fiesta', 'BMW_318i', 'Volkswagen_Polo', 'BMW_320i', 'Opel_Corsa', 'Renault_Twingo', 'Volkswagen_Golf', 'Opel_Corsa_1.2_16V']
common_brands = ['volkswagen', 'bmw', 'opel', 'mercedes_benz', 'audi', 'ford']
我想创建一个如下所示的词典:
{'volkswagen': ['Volkswagen_Golf_1.4', 'Volkswagen_Polo', 'Volkswagen_Golf'], 'bmw': ['BMW_316i', 'BMW_318i', 'BMW_320i'], 'opel': ['Opel_Corsa', 'Opel_Corsa_1.2_16V'],'ford': ['Ford_Fiesta'], 'Renault': ['Reanault_Twingo']}
使用我尝试过的代码,每个品牌只能获得一个型号,而且我找不到添加不在common_brands列表中的品牌的方法
models_by_brand = {}
for brand in common_brands:
for model in top_10:
if brand in model.lower():
models_by_brand[brand] = [model]
models_by_brand
输出:
{'bmw': ['BMW_320i'],
'ford': ['Ford_Fiesta'],
'opel': ['Opel_Corsa_1.2_16V'],
'volkswagen': ['Volkswagen_Golf']}
{'volkswagen': ['Volkswagen_Golf_1.4', 'Volkswagen_Polo', 'Volkswagen_Golf'],
'bmw': ['BMW_316i', 'BMW_318i', 'BMW_320i'],
'opel': ['Opel_Corsa', 'Opel_Corsa_1.2_16V'],
'ford': ['Ford_Fiesta']}
您可以使用并拆分车辆名称以获得品牌(如果这些名称是标准化的):
通过使用
defaultdict
,您可以按品牌[brand]编写模型。附加(模型)
,如果词典中当前没有品牌的模型,将创建并使用空列表。如果要保留代码的结构,请使用列表:
models_by_brand = {}
for brand in common_brands:
model_list=[]
for model in top_10:
if brand in model.lower():
model_list.append(model)
models_by_brand[brand] = model_list
models_by_brand = {k:v for k,v in models_by_brand.items() if v!=[]}
输出:
{'bmw': ['BMW_320i'],
'ford': ['Ford_Fiesta'],
'opel': ['Opel_Corsa_1.2_16V'],
'volkswagen': ['Volkswagen_Golf']}
{'volkswagen': ['Volkswagen_Golf_1.4', 'Volkswagen_Polo', 'Volkswagen_Golf'],
'bmw': ['BMW_316i', 'BMW_318i', 'BMW_320i'],
'opel': ['Opel_Corsa', 'Opel_Corsa_1.2_16V'],
'ford': ['Ford_Fiesta']}
@霍尔特的答案将是最有效的答案。解决问题的传统方法是检查字典中是否有关键字。如果没有,则将其初始化为空列表,否则附加到该列表
# The following code should work just fine.
top_10 = ['Volkswagen_Golf_1.4', 'BMW_316i', 'Ford_Fiesta', 'BMW_318i', 'Volkswagen_Polo', 'BMW_320i', 'Opel_Corsa',
'Renault_Twingo', 'Volkswagen_Golf', 'Opel_Corsa_1.2_16V']
common_brands = ['volkswagen', 'bmw', 'opel', 'mercedes_benz', 'audi', 'ford']
result = {}
cars = []
# For each car brand
for k in common_brands:
# For each car model
for c in top_10:
# if car brand present in car model name append it to list
if k.lower() in c.lower():
cars.append(c)
# if cars list is not empty copy it to the dictionary with key k
if len(cars) > 0:
result[k] = cars.copy()
# Reset cars list for next iteration
cars.clear()
print(result)
models_by_brand = {}
for brand in common_brands:
for model in top_10:
if brand in model.lower():
if brand not in models_by_brand:
models_by_brand[brand] = list()
models_by_brand[brand].append(model)
print(models_by_brand)
另一种解决方案是使用defaultdict。Defaultdict用于预先初始化任何键的默认值
from collections import defaultdict
models_by_brand = defaultdict(list)
for brand in common_brands:
for model in top_10:
if brand in model.lower():
models_by_brand[brand].append(model)
print(models_by_brand)
您想如何添加不在
普通品牌中的品牌?您确定车辆名称始终为品牌名称
?您可以使用设置默认值
将列表设置为不知道的钥匙的值。无论哪种方式,我都认为最好使用defaultdict
,但对某些人来说,它们有点让人困惑