简体   繁体   English

从字典列表制作嵌套字典

[英]Making nested dictionaries from a list of dictionaries

I've done some searching, playing and still can't seem to find a solution.我已经进行了一些搜索,播放,但似乎仍然无法找到解决方案。

Say I have a list of dictionaries such as:假设我有一个字典列表,例如:

listofdicts = [
    {'Name': 'Jim', 'Attribute': 'Height', 'Value': '6.3'},
    {'Name': 'Jim', 'Attribute': 'Weight', 'Value': '170'},
    {'Name': 'Mary', 'Attribute': 'Height', 'Value': '5.5'},
    {'Name': 'Mary', 'Attribute': 'Weight', 'Value': '140'}
]

But I want to place each of those in a singular named dictionary, such as:但我想将它们中的每一个放在一个单数命名字典中,例如:

listofdicts = [
    {'Person': {'Name': 'Jim', 'Attribute': 'Height', 'Value': '6.3'}},
    {'Person': {'Name': 'Jim', 'Attribute': 'Weight', 'Value': '170'}},
    {'Person': {'Name': 'Mary', 'Attribute': 'Height', 'Value': '5.5'}},
    {'Person': {'Name': 'Mary', 'Attribute': 'Weight', 'Value': '140'}}
]

How would I go about doing that?我将如何 go 这样做?

I've managed the following, but it only seems to nest one dictionary:我已经管理了以下内容,但它似乎只嵌套了一本字典:

betterlist = { 'Person' : i for i in listofdicts}

Any help would be appreciated.任何帮助,将不胜感激。 Thank you everyone.谢谢大家。

You're close, you're creating a list comprehension, not a dictionary comprehension你很接近,你正在创建一个列表理解,而不是字典理解

betterlist = [{ 'Person' : i} for i in listofdicts]

The reason "it only seems to nest one dictionary" is your dictionary comprehension is using the same key for every element, thus it overwrites previous values with the latest value for i “它似乎只嵌套一个字典”的原因是您的字典理解对每个元素使用相同的键,因此它用i的最新值覆盖以前的值

You almost got it right, but your outermost structure should be a list [] , while each element you construct is the dictionary {'Person': i} you created:您几乎做对了,但是您的最外层结构应该是一个列表[] ,而您构造的每个元素都是您创建的字典{'Person': i}

better_list = [
    {'Person': p}
    for p in list_of_dicts
]

What you wrote is called dict comprehension and only creates a single dictionary .您写的内容称为dict comprehension ,仅创建一个字典 As the keys in a dictionary only can occur once, each iteration i overwrites the single key 'Person' , so you end up with the person that happens to be last in your listofdicts .由于字典中的键只能出现一次,因此每次迭代i都会覆盖单个键'Person' ,因此您最终会得到恰好在listofdicts中最后一个的人。

So basically there's 2 (easy) way to do this.所以基本上有两种(简单的)方法可以做到这一点。

The clearest way is to create a loop:最清晰的方法是创建一个循环:

newDict = []
for d in listofdicts:
    newDict.append({'Person': d}) # I'm creating the dict, and then appending to newDict

There's a shortcut to do the above, called list comprehension .执行上述操作有一个捷径,称为list comprehension

newDict = [{'Person': d} for d in listofdicts] # Can you compare the 2 approach's syntax?

Understand that this is simply a shortcut.明白这只是一个捷径。 There's no performance improvement whatsoever.没有任何性能改进。 If I am new to Python, I would choose the first approach since it is extremely clear for me what it is doing.如果我是 Python 的新手,我会选择第一种方法,因为我非常清楚它在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM