使用标签列表创建字典字典
我正在尝试创建一个字典来标记我创建的给定字典。基本上,我创建了一个看起来像的列表,
labels = ["id1", "id2", "id3"] .
然后我有一个字典列表,看起来像,
dicts = [{'to': 2, 'give':1}, {'you': 1,'an':1}, {'example' :1}] .
我想要做的是创建一个新字典,正确标记每个字典,即 id1 与字典 1、id2 到字典 2 等一起使用。基本上制作一个新字典看起来像,
new_dict = {"id1" : {'to': 2, 'give':1}, "id2" : {'you': 1,'an':1}, "id3" : {'example' :1}}
我无法正确使用符号,这非常令人沮丧。有没有人有一个简单的解决方案?
回答
你可以这样做
labels = ["id1", "id2", "id3"]
dicts = [{'to': 2, 'give':1}, {'you': 1,'an':1}, {'example' :1}]
new_dict = dict(zip(labels, dicts))
print(new_dict)
//{'id2': {'you': 1, 'an': 1}, 'id3': {'example': 1}, 'id1': {'to': 2, 'give': 1}}