在python中合并字典列表

问题描述 投票:0回答:1

我有 6 本词典的列表:

[{'fruit':'Apple', 'Color':'red', 'weight':'10'},
{'fruit':'Banana', 'Color':'yellow', 'weight':'20'},
{'fruit':'Kiwi', 'Color':'Green', 'weight':'15'},
{'Veggie':'Onion', 'Color':'white', 'price':'10'},
{'Veggie':'Cabbage', 'Color':'Light Green', 'price':'30'},
{'Leafy':'Spinach', 'Color':'Dark Green', 'quantity':'30'}]

我需要将其转换为:

[{'Fruit':[Apple, Banana, Kiwi],'Color':[red, yellow, green],'weight':[10,20,15]},
 {'Veggie':[Onion, Cabbage],'Color':[white, light green], 'price':[10,30]},
 {'Leafy':[Spinach],'Color':[Dark Green],'quantity':[30]}]

我尝试了多种逻辑 - 比较连续的字典,使用映射来跟踪具有相同键的字典,但它们都不起作用。有人可以建议一下代码逻辑吗?谢谢你

python list dictionary for-loop
1个回答
0
投票
data = [
{'fruit': 'Apple', 'Color': 'red', 'weight': '10'},
{'fruit': 'Banana', 'Color': 'yellow', 'weight': '20'},
{'fruit': 'Kiwi', 'Color': 'Green', 'weight': '15'},
{'Veggie': 'Onion', 'Color': 'white', 'price': '10'},
{'Veggie': 'Cabbage', 'Color': 'Light Green', 'price': '30'},
{'Leafy': 'Spinach', 'Color': 'Dark Green', 'quantity': '30'}

] 嘿,我想是这样的:)

© www.soinside.com 2019 - 2024. All rights reserved.