繁体   English   中英

如何遍历字典列表并创建新字典

[英]How can I iterate over a list of dictionaries and create new dictionary

我正在尝试遍历下面的字典列表,其中另一个字典的“名称”==“名称”,价格(在本例中为“Variant OG”或“Bad Axe”的值)被放入一个字典。

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

如果您首先将 list_of_dictionaries 分成 2 个不同的列表,以便另一个列表包含所有“变体 OG”,而另一个包含所有“坏斧”,则可能会更容易。

然后你可以轻松地遍历它们,例如

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

看起来每个列表仅因Variant OGBad Axe而异,因此按itemitem_id排序并使用itertools.groupby迭代具有匹配键的项目将解决问题:

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}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM