我的字典在列表中为:
[{'c1': 'Cars ', 'c2': 'Class', 'keywords': 'muv'},
{'c1': 'Cars ', 'c2': 'Class', 'keywords': 'hatchback'},
{'c1': 'Cars ', 'c2': 'Class', 'keywords': 'suv'},
{'c1': 'Cars ', 'c2': 'Class', 'keywords': 'sedan'},
{'c1': 'Cars ', 'c2': 'Class', 'keywords': 'coupe'},
{'c1': 'Cars ', 'c2': 'FuelType', 'keywords': 'electric'},
{'c1': 'Cars ', 'c2': 'FuelType', 'keywords': 'diesel'},
{'c1': 'Cars ', 'c2': 'FuelType', 'keywords': 'cng'},
{'c1': 'Cars ', 'c2': 'FuelType', 'keywords': 'petrol'}]
我想将键keywords
的值存储到字典中,键c1
和c2
的值对与键名c1#c2
相同。所以预期的输出是这样的:
cars= {'Cars#Class':
['muv','hatchback','suv','sedan','coupe'],
'Cars#FuelType':
['electric','diesel','cng','petrol']}
因此,当我使用cars['Cars#Class']
时会给我['muv','hatchback','suv','sedan','coupe']
。
答案 0 :(得分:2)
您可以将collections.defaultdict
与迭代一起使用。给定字典L
的输入列表:
from collections import defaultdict
d = defaultdict(list)
for i in L:
d[i['c1'].strip()+'#'+i['c2']].append(i['keywords'])
结果:
print(d)
defaultdict(list,
{'Cars#Class': ['muv', 'hatchback', 'suv', 'sedan', 'coupe'],
'Cars#FuelType': ['electric', 'diesel', 'cng', 'petrol']})
答案 1 :(得分:1)
如果您乐于使用第三方库,则可以使用熊猫。 pd.DataFrame
构造函数直接接受字典列表。给定一个输入列表L
:
import pandas as pd
df = pd.DataFrame(L)
d = df.groupby(df['c1'].str.strip()+'#'+df['c2'])['keywords']\
.apply(list).to_dict()
print(d)
{'Cars#Class': ['muv', 'hatchback', 'suv', 'sedan', 'coupe'],
'Cars#FuelType': ['electric', 'diesel', 'cng', 'petrol']}
答案 2 :(得分:0)
您可以使用itertools.goupby来做到这一点。
import itertools
l = [{'c1': 'Cars ', 'c2': 'Class', 'keywords': 'muv'},
{'c1': 'Cars ', 'c2': 'Class', 'keywords': 'hatchback'},
{'c1': 'Cars ', 'c2': 'Class', 'keywords': 'suv'},
{'c1': 'Cars ', 'c2': 'Class', 'keywords': 'sedan'},
{'c1': 'Cars ', 'c2': 'Class', 'keywords': 'coupe'},
{'c1': 'Cars ', 'c2': 'FuelType', 'keywords': 'electric'},
{'c1': 'Cars ', 'c2': 'FuelType', 'keywords': 'diesel'},
{'c1': 'Cars ', 'c2': 'FuelType', 'keywords': 'cng'},
{'c1': 'Cars ', 'c2': 'FuelType', 'keywords': 'petrol'}]
result = {}
for key, group in itertools.groupby(l, lambda x: "{}#{}".format(x['c1'].strip(), x['c2'].strip())):
result[key] = map(lambda x: x['keywords'], list(group))
print(result)
答案 3 :(得分:0)
result = {}
for d in ld:
val_c1 = d['c1']
val_c2 = d['c2']
val_key = d['keywords']
combined_key = val_c1+'#'+val_c2
if combined_key not in result:
result[combined_key] = []
result[combined_key].append(val_key)
else:
result[combined_key].append(val_key)
答案 4 :(得分:0)
使用itertools.groupby
根据键'c2'的值对字典进行分组,并从分组的字典中提取键keywords
的值
>>> lst = [{'c1': 'Cars ', 'c2': 'Class', 'keywords': 'muv'}, {'c1': 'Cars ', 'c2': 'Class', 'keywords': 'hatchback'}, {'c1': 'Cars ', 'c2': 'Class', 'keywords': 'suv'}, {'c1': 'Cars ', 'c2': 'Class', 'keywords': 'sedan'}, {'c1': 'Cars ', 'c2': 'Class', 'keywords': 'coupe'}, {'c1': 'Cars ', 'c2': 'FuelType', 'keywords': 'electric'}, {'c1': 'Cars ', 'c2': 'FuelType', 'keywords': 'diesel'}, {'c1': 'Cars ', 'c2': 'FuelType', 'keywords': 'cng'}, {'c1': 'Cars ', 'c2': 'FuelType', 'keywords': 'petrol'}]
>>> cars = {f'Cars#{k}':[d['keywords'] for d in v] for k,v in groupby(lst, lambda d: d['c2'])}
>>> print(cars)
{'Cars#Class': ['muv', 'hatchback', 'suv', 'sedan', 'coupe'], 'Cars#FuelType': ['electric', 'diesel', 'cng', 'petrol']}
>>> print(cars['Cars#Class'])
['muv', 'hatchback', 'suv', 'sedan', 'coupe']
>>>