简体   繁体   English

创建具有相同键的字典列表?

[英]Creating a list of dictionaries with same keys?

I wanted to create a list that contains x amount of dictionaries all containing the same keys but with different values that's made in a for loop:我想创建一个列表,其中包含 x 个字典,所有字典都包含相同的键,但在 for 循环中具有不同的值:

Something like就像是

[{'name': Brenda, 'Age': 22, 'Sex': Female},
 {'name': Jorda, 'Age': 32, 'Sex': Male},
 {'name': Richard, 'Age': 54, 'Sex': Male}]

My code is this:我的代码是这样的:

people = []
person = {}

humans = gethumans()

    for human in humans:
        number_people, people_data = People.data()
        person['name'] = human.name
        person['age'] = human.age
        person['Sex'] = human.name
        people.append(person)

My output is something like this:我的输出是这样的:

[{'name': Richard, 'Age': 54, 'Sex': Male},
 {'name': Richard, 'Age': 54, 'Sex': Male}
 {'name': Richard, 'Age': 54, 'Sex': Male}]

Since the dictionary values are getting replaced and not added and it's just appending the same dicitnary.由于字典值被替换而不是添加,它只是附加相同的字典。 How can I get around this?我怎样才能解决这个问题?

When you append the dictionary person to the list people you are just appending a reference to the dictionary to the list, so the list ends up containing just references to the SAME dictionary.当您将字典person附加到列表people 时,您只是将对该字典的引用附加到列表中,因此该列表最终仅包含对 SAME 字典的引用。

Since each time through the loop you overwrite the dictionary with new values, at the end the list contains just references to the last person you appended.由于每次循环时您都会用新值覆盖字典,因此列表最后只包含对您添加的最后一个人的引用。

What you need to do is create a new dictionary for every person, for example:您需要做的是为每个人创建一个新字典,例如:

for human in humans:
    number_people, people_data = People.data()
    person = dict()
    person['name'] = human.name
    person['age'] = human.age
    person['Sex'] = human.name
    people.append(person)

You each time edit the same dictionary, so you do not construct a new one, but edit the old one.您每次都编辑同一个字典,因此您不会构建新字典,而是编辑旧字典。 Since you each time append the same dictionary to the list, in the end the list contains the same dictionary n times, and all edits are processed on that dictionary.由于您每次都将相同的字典附加到列表中,因此列表最后包含相同的字典n次,并且所有编辑都在该字典上进行处理。

You thus have to construct a new dictionary in each iteration in the for loop:因此,您必须在for循环的每次迭代中构建一个新字典:

people = []
humans = gethumans()

for human in humans:
    number_people, people_data = People.data()
    person = {
        'name': human.name,
        'age': human.age,
        'sex': human.sex
    }
    people.append(person)

I here replaced 'Sex' with 'sex' (since it is strange to have inconsistent key names), and used human.sex instead of human.name .我在这里代替'Sex''sex' (因为它是奇怪,有不一致的键名),以及用于human.sex代替human.name

Here people.data() do not seem to do anything, you can thus use list comprehension here to generate the list:这里people.data()似乎没有做任何事情,因此您可以在这里使用列表理解来生成列表:

people = [
    { 'name': human.name, 'age': human.age, 'sex': human.sex }
    for human in humans
]

This will construct a list with all the dictionaries.这将构建一个包含所有字典的列表。 Given the for loop had no side-effects (this looks to be the case), the above will work.鉴于for循环没有副作用(看起来确实如此),上面的方法将起作用。

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

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