简体   繁体   English

当原始字典的值为列表时创建字典列表

[英]Create list of dictionaries when values of original dictionary are lists

I have a dictionary where the values for each key are a list.我有一本字典,其中每个键的值都是一个列表。 I would like to extract a key value pair for each key in the dictionary with its corresponding element value from the list, and then store individual dictionaries in a new list.我想从列表中为字典中的每个键及其对应的元素值提取一个键值对,然后将各个字典存储在一个新列表中。

So, if I have a simple dictionary like the following:所以,如果我有一个像下面这样的简单字典:

my_dict = {'Folder': ['2021-03-12_020000', '2021-03-12_020000', '2021-03-12_020000'],
           'Filename': ['2021-03-12_020000-frame79.jpg', '2021-03-12_020000-frame1.jpg', '2021-03- 
                       12_020000-frame39.jpg'],  
           'Labeler': ['Labeler 2', 'Labeler 2', 'Labeler 1']}

My desired output list would be a list of dictionaries, where each dictionary has the keys from my_dict with the corresponding value from the original values lists in my_dict:我想要的 output 列表将是一个字典列表,其中每个字典都有来自 my_dict 的键以及来自 my_dict 中原始值列表的相应值:

final_list = [{'Folder':'2021-03-12_020000', 'Filename': '2021-03-12_020000-frame79.jpg', 'Labeler': 'Labeler 2'},
            {'Folder':'2021-03-12_020000', 'Filename': '2021-03-12_020000-frame1.jpg', 'Labeler': 'Labeler 2'},
            {'Folder':'2021-03-12_020000', 'Filename': '2021-03-12_020000-frame39.jpg', 'Labeler': 'Labeler 1'}]

for dynamic key and value:对于动态键和值:

my_dict = {'Folder': ['2021-03-12_020000', '2021-03-12_020000', '2021-03-12_020000'],
           'Filename': ['2021-03-12_020000-frame79.jpg', '2021-03-12_020000-frame1.jpg', '2021-03- 12_020000-frame39.jpg'],  
           'Labeler': ['Labeler 2', 'Labeler 2', 'Labeler 1']}

new_dict = {}
for key in my_dict:
    for idx, value in enumerate(my_dict[key]):
        if idx not in new_dict:
            new_dict[idx] = [(key,value)]
        else:
            new_dict[idx].append((key,value))
new_list = []
for key,value in new_dict.items():
    new_list.append(dict(value))

print(new_list)

output output

[{'Folder': '2021-03-12_020000', 'Filename': '2021-03-12_020000-frame79.jpg', 'Labeler': 'Labeler 2'}, {'Folder': '2021-03-12_020000', 'Filename': '2021-03-12_020000-frame1.jpg', 'Labeler': 'Labeler 2'}, {'Folder': '2021-03-12_020000', 'Filename': '2021-03- 12_020000-frame39.jpg', 'Labeler': 'Labeler 1'}]

You can use zip for the task:您可以使用zip来完成任务:

my_dict = {
    "Folder": ["2021-03-12_020000", "2021-03-12_020000", "2021-03-12_020000"],
    "Filename": [
        "2021-03-12_020000-frame79.jpg",
        "2021-03-12_020000-frame1.jpg",
        "2021-03-12_020000-frame39.jpg",
    ],
    "Labeler": ["Labeler 2", "Labeler 2", "Labeler 1"],
}

final_list = [
    {"Folder": a, "Filename": b, "Labeler": c}
    for a, b, c in zip(
        my_dict["Folder"], my_dict["Filename"], my_dict["Labeler"]
    )
]

print(final_list)

Prints:印刷:

[
    {
        "Folder": "2021-03-12_020000",
        "Filename": "2021-03-12_020000-frame79.jpg",
        "Labeler": "Labeler 2",
    },
    {
        "Folder": "2021-03-12_020000",
        "Filename": "2021-03-12_020000-frame1.jpg",
        "Labeler": "Labeler 2",
    },
    {
        "Folder": "2021-03-12_020000",
        "Filename": "2021-03-12_020000-frame39.jpg",
        "Labeler": "Labeler 1",
    },
]

Here is code assuming all items are inter related.这是假设所有项目都是相互关联的代码。

my_dict = {'Folder': ['2021-03-12_020000', '2021-03-12_020000', '2021-03-12_020000'],
           'Filename': ['2021-03-12_020000-frame79.jpg', '2021-03-12_020000-frame1.jpg', '2021-03- 12_020000-frame39.jpg'],  
           'Labeler': ['Labeler 2', 'Labeler 2', 'Labeler 1']}

result_dict = []

for i, (folder, filename, labler) in enumerate(zip(my_dict['Folder'], my_dict['Filename'], my_dict['Labeler'])):
    result_dict.append({"Folder":folder,'Filename':filename,"Labeler":labler })

print(result_dict)

output: output:

[
  {
    "Folder": "2021-03-12_020000",
    "Filename": "2021-03-12_020000-frame79.jpg",
    "Labeler": "Labeler 2"
  },
  {
    "Folder": "2021-03-12_020000",
    "Filename": "2021-03-12_020000-frame1.jpg",
    "Labeler": "Labeler 2"
  },
  {
    "Folder": "2021-03-12_020000",
    "Filename": "2021-03- 12_020000-frame39.jpg",
    "Labeler": "Labeler 1"
  }
]

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

相关问题 迭代字典值,这些列表值是用于创建新的字典列表的列表 - Iterating over dictionary values which are lists to create a new list of dictionaries 从Python中的字典列表创建列表字典 - Create Dictionary Of Lists from List of Dictionaries in Python 字典列表到列表字典 - list of dictionaries to dictionary of lists 如何从字典列表中创建三个单独的值列表,其中每个字典都有三个键 - How to create three separate lists of values from a list of dictionaries where each dictionary has three keys 如何使用字典列表作为字典列表中的值创建字典 - how to create a dictionary with list of dictionaries as values from a list of dictionaries 如何使用列表列表和元组列表创建字典字典 - How to create a dictionary of dictionaries using a list of lists and a list of tuples 将字典列表写到csv中,其中字典值是列表 - Write a list of dictionaries to csv where the dictionary values are lists 如何将具有任意数量值的列表字典拆分为字典列表? - How to split a dictionary of lists with arbitrary number of values into a list of dictionaries? 如何从具有不同长度列表的字典创建字典列表 - How to create a list of dictionaries from a dictionary with lists of different lengths 将列表列表转换为词典字典 - Convert list of lists to dictionary of dictionaries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM