python newbe here。我看过堆栈溢出,但无法找到与此类似的问题。我想合并具有相同键和值的字典列表是相同的(所以在我的情况下合并的名称是相同的)。如何合并具有相同键的字典列表,其值相同?
这是我当前列表:
current = [
{'name' : 'food festival', 'category' : ['Miscellaneous', 'Undefined'], 'venue' : 'venue_1', 'price_1' : 100, 'price_2' : 120, 'start' : '2017-10-04T14:30:00Z'},
{'name' : 'food festival', 'category' : ['Miscellaneous', 'Undefined'], 'venue' : 'venue_2', 'price_1' : 150, 'price_2' : 200, 'start' : '2017-11-04T14:30:00Z'},
{'name' : 'music festival', 'category': ['music', 'pop'], 'venue' : 'venue_3', 'price_1' : 300, 'price_2' : 320, 'start' : '2017-12-04T14:30:00Z'}
]
而这就是我想实现:
final = [
{
'name': 'food festival',
'category': ['Miscellaneous', 'Undefined'],
'shows': [
{
'start': '2017-10-04T14:30:00Z',
'venue': 'venue_1',
'prices': [
{ 'price_1' : 100 },
{ 'price_2' : 120}
]
},
{
'start': '2017-11-04T14:30:00Z',
'venue': 'venue_2',
'prices': [
{ 'price_1': 150 },
{ 'price_2' : 200 }
]
}
]
},
{
'name': 'music festival',
'category': ['music', 'pop'],
'shows': [
{
'start': '2017-12-04T14:30:00Z',
'venue': 'venue_3',
'prices': [
{ 'price_1' : 300 },
{ 'price_2' : 320}
]
}
]
}
]
您对“价格”键改变看起来比刚才键合并更复杂..你确定你想要“价格”是一个长度单一的列表? –
是的,这将是最想要的结果 - 也许我应该在标题和解释中更清楚一点。 –
我想@IzaakvanDongen,为什么不使用一个列表和索引是价格? 'prices':[300,320'] – SD3L