简体   繁体   English

如何从 python 中的列表列表中创建字典列表?

[英]How to create a list of dictionaries from the list of lists in python?

There is the list of lists, named "people": people=[['Sara Smith', 42, 'female', 35000], ['John Lee', 25, 'male', 25000]] I'd like to create the list of dictionaries: person=[{'name:'Sara Smith', 'age':42, 'gender':'female', 'income':35000}, {'name:'John Lee', 'age':25, 'gender':'male', 'income':25000}] How can I do it?有一个名为“people”的列表列表: people=[['Sara Smith', 42, 'female', 35000], ['John Lee', 25, 'male', 25000]]我想创建字典列表: person=[{'name:'Sara Smith', 'age':42, 'gender':'female', 'income':35000}, {'name:'John Lee', 'age':25, 'gender':'male', 'income':25000}]我该怎么做呢?

I tried this:我试过这个:

fields=['name', 'age', 'gender', 'income']
print(len(people))
for i in range(0, len(people)):
    exec("people%d = %s" % (i + 1, repr(people[i])));
    person=[]
    person.append(dict(zip(fields, people[i])))
    print(people[i])
print(person)

But for some reason, as a result of "print(person)" I have [{'name': 'John Lee', 'age': 25, 'gender': 'male', 'income': 25000}].但由于某种原因,由于“打印(人)”,我有 [{'name': 'John Lee', 'age': 25, 'gender': 'male', 'income': 25000}]。 I cannot understand, why my result includes only a dictionary for people[1] in the list and maybe there are some more elegant solutions for the task我无法理解,为什么我的结果在列表中只包含一个人 [1] 的字典,也许有一些更优雅的任务解决方案

You could use dict() and zip fields into each using a list comprehension.您可以使用列表理解将dict()zip fields用于每个字段。

people=[['Sara Smith', 42, 'female', 35000], ['John Lee', 25, 'male', 25000]]
fields = ['name', 'age', 'gender', 'income']

dict_people = [dict(zip(fields, l)) for l in people]

Result:结果:

[{'name': 'Sara Smith', 'age': 42, 'gender': 'female', 'income': 35000}, {'name':'John Lee', 'age': 25, 'gender': 'male','income': 25000}]

This is a much cleaner way to do it这是一种更清洁的方法

people=[['Sara Smith', 42, 'female', 35000], ['John Lee', 25, 'male', 25000]]
fields=['name', 'age', 'gender', 'income']
person=[]
for i in people:
    person.append(dict(zip(fields, i)))
print(person)

Output is: Output 是:

[{'name': 'Sara Smith', 'age': 42, 'gender': 'female', 'income': 35000}, {'name': 'John Lee', 'age': 25, 'gender': 'male', 'income': 25000}] [{'name': 'Sara Smith', 'age': 42, 'gender': 'female', 'income': 35000}, {'name': 'John Lee', 'age': 25, 'gender ':'男性','收入':25000}]

You can actually do this all with a single dictionary comprehension.你实际上可以用一个字典理解来完成这一切。 I'd really really recommend avoiding using exec unless you absolutely need to.除非绝对需要,否则我真的建议避免使用exec

Try something like this instead:尝试这样的事情:

person_dicts = [{"name": person[0], "age": person[1], "gender": person[2], "income": person[3]} for person in people]

Outputs:输出:

[{'name': 'Sara Smith', 'age': 42, 'gender': 'female', 'income': 35000}, {'name': 'John Lee', 'age': 25, 'gender': 'male', 'income': 25000}]

Although this assumes your people list's sublist for a single person is always ordered the same way.尽管这假设您的人员列表的单个人员的子列表始终以相同的方式排序。

Edit:编辑:

If you don't want to use comprehensions you can always just use a for loop too.如果您不想使用推导式,您也可以随时使用 for 循环。

person_dicts = []
for person in people:
    person_dicts.append({"name": person[0], "age": person[1], "gender": person[2], "income": person[3]})

You can do with zip as mentioned before:如前所述,您可以使用 zip :

people=[['Sara Smith', 42, 'female', 35000], ['John Lee', 25, 'male', 25000]]
fields=['name', 'age', 'gender', 'income']

persons = []
for person in people:
    tmp_dict = {}
    for key, value in zip(fields, person):
        tmp_dict[key] = value
    persons.append(tmp_dict)

Results:结果:

[
  {'age': 42, 'gender': 'female', 'income': 35000, 'name': 'Sara Smith'},
  {'age': 25, 'gender': 'male', 'income': 25000, 'name': 'John Lee'},
]

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

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