【问题标题】:How to create a list of dictionaries from the list of lists in python?如何从python中的列表列表中创建字典列表?
【发布时间】:2019-11-06 16:30:12
【问题描述】:

有列表列表,名为“人”: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}] 我该怎么做?

我试过这个:

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)

但由于某种原因,“print(person)”的结果是 [{'name': 'John Lee', 'age': 25, 'gender': 'male', 'income': 25000 }]。我不明白,为什么我的结果在列表中只包含一个人 [1] 的字典,也许该任务有一些更优雅的解决方案

【问题讨论】:

  • 您需要将person=[] 移动到for 循环上方。

标签: python list dictionary


【解决方案1】:

您可以使用列表理解将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]

结果:

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

【讨论】:

  • 虽然这样做您不会保留nameagegenderincome 字段。你最终得到name:agegender:income
【解决方案2】:

这是一种更清洁的方法

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)

输出是:

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

【讨论】:

    【解决方案3】:

    您实际上可以用一个字典理解来完成这一切。我真的非常建议避免使用exec,除非你绝对需要。

    试试这样的方法:

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

    输出:

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

    虽然这假设您的人员列表的单个人员的子列表始终以相同的方式排序。

    编辑:

    如果您不想使用推导式,也可以随时使用 for 循环。

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

    【讨论】:

    • 感谢大家的想法。最后我通过pers_dicts = [dict(zip(fields, person)) for person in people]解决了
    【解决方案4】:

    你可以像前面提到的那样使用 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)
    

    结果:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      • 2022-11-18
      • 2018-12-17
      相关资源
      最近更新 更多