Python 将字典列表中的对象分组

Python 将字典列表中的对象分组,python,json,dictionary,Python,Json,Dictionary,我不知道如何清楚地解释这一点,但我有一个来自API的JSON结果,该API返回未知数量的产品,每个产品都有单独的条目以及3种不同服务的价格 然后我需要将这些组作为一个组进行迭代处理 例如,数据集 [ { 'name': 'widgetb', 'type': 'ebay' } { 'name': 'widgeta', 'type': 'amazon' } { 'name': 'widgetc', 'type': 'newegg' } { 'name': 'widgeta', 'ty

我不知道如何清楚地解释这一点,但我有一个来自API的JSON结果,该API返回未知数量的产品,每个产品都有单独的条目以及3种不同服务的价格

然后我需要将这些组作为一个组进行迭代处理

例如,数据集

[
  { 'name': 'widgetb', 'type': 'ebay' }
  { 'name': 'widgeta', 'type': 'amazon' }
  { 'name': 'widgetc', 'type': 'newegg' }
  { 'name': 'widgeta', 'type': 'newegg' }
  { 'name': 'widgetb', 'type': 'newegg' }
  { 'name': 'widgeta', 'type': 'ebay' }
  { 'name': 'widgetc', 'type': 'amazon' }
  { 'name': 'widgetb', 'type': 'amazon' }
  { 'name': 'widgetc', 'type': 'ebay' }
]
有两个API结果集,第一个为您提供可用产品的列表,以及与服务配对的唯一条目(Amazon、eBay、Newegg)。因此,对于每个产品,有3个条目,但它们不符合顺序

第二个API返回产品服务和价格。 示例价格结果将是

[
    ProductC-NewEgg: Price
    ProdutA-NewEgg: Price
    ProductB-NewEgg: Price
    ProductA-Amazon: Price
    ProductA-Ebay: Price
    ProductC-Amazon: Price
    ProductB-Ebay: Price
    ProductC-Ebay: Price
    ProductB-Amazon: Price
]
我最初的想法是从第一个API中获得所有产品的唯一列表,并将它们放在一个列表中。然后检查每个产品/服务组合,并填写存储在每个产品列表元素下的字典

因此,结果将类似于>

[
Product A { price a, b c }
Product B { price a, b c }
Product C { price a, b c }
]

我觉得我做得不对。

不确定我是否清楚您的要求。但看看这个:

>>> first_api_result = [
  { 'name': 'widgetb', 'type': 'ebay' }
  ,{ 'name': 'widgeta', 'type': 'amazon' }
  ,{ 'name': 'widgetc', 'type': 'newegg' }
  ,{ 'name': 'widgeta', 'type': 'newegg' }
  ,{ 'name': 'widgetb', 'type': 'newegg' }
  ,{ 'name': 'widgeta', 'type': 'ebay' }
  ,{ 'name': 'widgetc', 'type': 'amazon' }
  ,{ 'name': 'widgetb', 'type': 'amazon' }
  ,{ 'name': 'widgetc', 'type': 'ebay' }
]
>>> second_api_result=[
    {'widgeta-NewEgg': 10 }
    ,{'widgetb-NewEgg': 20  }
    ,{'widgetc-NewEgg': 30 }
    ,{'widgetb-Amazon': 35 }
    ,{'widgetb-Ebay': 40   }
    ,{'widgeta-Amazon': 50 }
    ,{'widgetc-Ebay': 60   }
    ,{'widgeta-Ebay': 70   }
    ,{'widgetc-Amazon': 80 }
]
>>> result = {d['name']:[]for d in first_api_result}
>>> for price_dict in second_api_result:
        product,price = price_dict.items()[0]
        result[product[:product.index('-')]].append(price)


>>> result
{'widgetc': [30, 60, 80], 'widgetb': [20, 35, 40], 'widgeta': [10, 50, 70]}

您的数据集不包含价格。请添加足够的示例输入数据,以便我们能够了解它与预期结果日期的关系。您试图实现的目标是什么?把价格组合在一起?第一个结果是什么,从您描述的内容来看,这听起来是多余的。您应该尝试创建实际的示例数据。我需要获得每个产品的所有价格,然后遍历每个产品,比较它们,并显示它们如何相关的一些数据(即高5%,低1%)而不是基线。所以这里不是为您编写代码的。即使是这样,你的例子也不是有效的语法,所以很难说出你在问什么。这基本上是我的方向,我使用字典和字典来计算三种价格,这样我以后可以添加更多,我可以按名称引用它们。对我来说似乎非常黑客,而且我的python经验非常丰富,所以我不确定我是否错过了一些让这更容易的很棒的特性。