基于键的字典组列表

Group list of dictionaries based on key

我有一个看起来像这样的数组

comp_data = [
  {
    "product_id": 432263,
    "price_zone_id": 1,
    "oreilly_price": 0,
    "amazon_price": 0,
    "az_price": 0,
    "rockauto_price": 0,
    "napa_price": 0,
    "oreilly_index": 0,
    "amazon_index": 0,
    "az_index": 0,
    "rockauto_index": 0,
    "napa_index": 0,
    "weighted_comp_price": 0,
    "weighted_comp_index": 0,
    "week": None
  }
]

跳过字段 product_id 和 price_zone_id,我想通过映射每个键的名称来创建字典列表。例如

大家可以看到amazon_price,amazon_index,最终我想要一个这样的列表

[
    {
        'amazon_price': 0,
        'amazon_index': 0,
        'name': 'Amazon' --> This would be simply adding .title() to the name. 
    },
    {
        'az_price': 0,
        'az_index': 0,
        'name': 'Az' --> This would be simply adding .title() to the name. 
    },
    {
        'orielly_price': 0,
        'orielly_index': 0,
        'name': 'ORielly' --> This would be simply adding .title() to the name. 
    }
]

我当前的代码看起来像这样并且生成了错误的输出

stores_data = {}
for data in comp_data:
    dict_keys = data.keys()
    for keys in dict_keys:
        if keys != 'product_id' and keys != 'price_zone_id':
            store_name = keys.split('_')[0]
            value_type = keys.split('_')[-1]
            stores[store_name][value_type] =  {}

商店本质上是 _index 或 _price 前面的字符串。例如 amazon_index,商店将是亚马逊

对于此关键字 "weighted_comp_price",商店将是加权补偿

您的代码正在创建“价格”和“索引”,而不是整个名称。你想要哪个?

comp_data = [
  {
    "product_id": 432263,
    "price_zone_id": 1,
    "oreilly_price": 0,
    "amazon_price": 0,
    "az_price": 0,
    "rockauto_price": 0,
    "napa_price": 0,
    "oreilly_index": 0,
    "amazon_index": 0,
    "az_index": 0,
    "rockauto_index": 0,
    "napa_index": 0,
    "weighted_comp_price": 0,
    "weighted_comp_index": 0,
    "week": None
  }
]

stores = {}
for data in comp_data:
    for key,value in data.items():
        if key not in ('product_id','price_zone_id','week'):
            words = key.split('_')
            store_name ='_'.join(words[:-1])
            value_type = words[-1]
            if store_name not in stores:
                stores[store_name] = {'name': store_name.title()}
            stores[store_name][key] = value

from pprint import pprint
pprint(stores)

输出:

{'amazon': {'amazon_index': 0, 'amazon_price': 0, 'name': 'Amazon'},
 'az': {'az_index': 0, 'az_price': 0, 'name': 'Az'},
 'napa': {'name': 'Napa', 'napa_index': 0, 'napa_price': 0},
 'oreilly': {'name': 'Oreilly', 'oreilly_index': 0, 'oreilly_price': 0},
 'rockauto': {'name': 'Rockauto', 'rockauto_index': 0, 'rockauto_price': 0},
 'weighted_comp': {'name': 'Weighted_Comp',
                   'weighted_comp_index': 0,
                   'weighted_comp_price': 0}}

每个 dict

  1. 找到 _price
  2. 确认您拥有相应的 _index 密钥
  3. 保存
stores_data = []
for data in comp_data:
    for price_key in (k for k in data if k.endswith("_price")):
        name = price_key.rsplit("_", maxsplit=1)[0]
        index_key = f'{name}_index'
        if index_key in data:
            stores_data.append({price_key: data[price_key], index_key: data[index_key], 'name': name.title()})

给予

[{'oreilly_price': 0, 'oreilly_index': 0, 'name': 'Oreilly'}, 
 {'az_price': 0, 'az_index': 0, 'name': 'Az'}, 
 {'napa_price': 0, 'napa_index': 0, 'name': 'Napa'}, 
 {'amazon_price': 0, 'amazon_index': 0, 'name': 'Amazon'}, 
 {'rockauto_price': 0, 'rockauto_index': 0, 'name': 'Rockauto'}, 
 {'weighted_comp_price': 0, 'weighted_comp_index': 0, 'name': 'Weighted_Comp'}]