如何迭代字典列表并创建新字典

2024-10-03 23:25:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在下面的字典列表中进行迭代,如果另一个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}

Tags: idgroupitemproductionmenuogbadvariant
2条回答

看起来每个列表只随Variant OGBad Axe而变化,因此按itemitem_id排序并使用^{}对具有匹配键的项进行迭代将解决问题:

from itertools import groupby

lst = [{'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}]


# groupby requires the container to be sorted by the key
sortkey = lambda x: x['item_id']
lst.sort(key=sortkey)

# "key" is the common key for the items
# "items" is an iterator of the common items
for key,items in groupby(lst,key=sortkey):
    # start with an empty dict for each common key
    dct = {}
    for item in items:
        # add all key/value pairs for each common key item to the dict
        dct.update(item)
    print(dct)

输出:

{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1334, 'item': 'Hot Bar Serving Hardware', 'Variant OG': 5.0, '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}

如果您首先将字典列表划分为两个不同的列表,以便另一个包含所有的“变体OG”,另一个包含所有的“坏斧头”,则会更容易

然后,您可以轻松地遍历它们

for variant in variant_OGs:
       for bad in bad_axes:
             if( variant['item']==bad['item']):
                   #put them into single dict

相关问题 更多 >