我试图在下面的字典列表中进行迭代,如果另一个dict的'name'='name',则价格(在本例中是'Variant OG'或'Bad Axe'的值)被放入一个dict中
[{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1354, 'item': 'Giardiniera', 'Variant OG': 5.0},
{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1355, 'item': 'Sweet Peppers', 'Variant OG': 5.0},
{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1334, 'item': 'Hot Bar Serving Hardware', 'Variant OG': 5.0},
{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1354, 'item': 'Giardiniera', 'Bad Axe': 1.0},
{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1355, 'item': 'Sweet Peppers', 'Bad Axe': 1.0},
{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1334, 'item': 'Hot Bar Serving Hardware', 'Bad Axe': 1.0}]
基本上,所有相同的菜单项都在同一个目录中,但也包含不同的价格
理想情况下,最终结果如下
{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1354, 'item': 'Giardiniera', 'Variant OG': 5.0, 'Bad Axe': 1.0}
{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1355, 'item': 'Sweet Peppers', 'Variant OG': 5.0, 'Bad Axe': 1.0}
{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1334, 'item': 'Hot Bar Serving Hardware', 'Variant OG': 5.0, 'Bad Axe': 1.0}
看起来每个列表只随} 对具有匹配键的项进行迭代将解决问题:
Variant OG
或Bad Axe
而变化,因此按item
或item_id
排序并使用^{输出:
如果您首先将字典列表划分为两个不同的列表,以便另一个包含所有的“变体OG”,另一个包含所有的“坏斧头”,则会更容易
然后,您可以轻松地遍历它们
相关问题 更多 >
编程相关推荐