我有一个看起来像这样的数组
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 前面的字符串。ex amazon_index,商店将是亚马逊
对于这个键 "weighted_comp_price"
,商店将是加权比较