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



我有一个字典,其中每个键的值是一个列表。我想为字典中的每个键提取一个键值对,并从列表中提取相应的元素值,然后将各个字典存储在一个新列表中。

那么,如果我有一个像这样的简单字典:

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_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'}]

动态键和值:

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)

输出
[{'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'}]

假设所有项都是相互关联的

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)

输出:

[
{
"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"
}
]

您可以使用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)

打印:

[
{
"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",
},
]

相关内容

  • 没有找到相关文章