【问题标题】:How do I match a list with a dict and time related values?如何将列表与字典和时间相关值匹配?
【发布时间】:2021-02-22 16:57:19
【问题描述】:

我在我的views.py

# Get the amount of kilo attached to products
product_data = {}
for product in ProductSpy.objects.all():
    product_data[product.id] = {"product_id": product.product_id, "kilo": product.kilo}

# Get quantity bought of each product
total_qty_bought = self.order_item_model.objects.values("product").annotate(Sum("quantity"))

​ ​​​ 所以在我的product_data 转储中,我在这里得到了这个:

6: {'product_id': 32, 'kilo': Decimal('25.00')},

在我的 total_qty_bought 转储中,我得到了这个:

{'product': 32, 'quantity__sum': Decimal('5')},

现在product_id 匹配product,我希望它返回125,因为我们得到5 乘以25 公斤的数量。 在我的total_qty_bought 转储中,您会发现 id 不相关,因此如果它们与我的product_data 转储中的任何内容都不匹配,我希望它们被忽略。

实现这一目标的最聪明方法是什么?

打印转储:

产品数据:

{4: {'product_id': 30, 'kilo': Decimal('25.00')}, 5: {'product_id': 31, 'kilo': Decimal('25.00')}, 6: {'product_id': 32, 'kilo': Decimal('25.00')}, 7: {'product_id': 33, 'kilo': Decimal('25.00')}, 8: {'product_id': 37, 'kilo': Decimal('15.00')}, 9: {'product_id': 38, 'kilo': Decimal('15.00')}, 10: {'product_id': 39, 'kilo': Decimal('10.00')}, 11: {'product_id': 40, 'kilo': Decimal('10.00')}, 12: {'product_id': 41, 'kilo': Decimal('5.00')}, 13: {'product_id': 42, 'kilo': Decimal('2.50')}, 14: {'product_id': 43, 'kilo': Decimal('5.00')}, 15: {'product_id': 44, 'kilo': Decimal('2.50')}, 17: {'product_id': 50, 'kilo': Decimal('2.50')}, 18: {'product_id': 51, 'kilo': Decimal('5.00')}, 19: {'product_id': 52, 'kilo': Decimal('10.00')}, 20: {'product_id': 53, 'kilo': Decimal('15.00')}, 21: {'product_id': 55, 'kilo': Decimal('15.00')}, 22: {'product_id': 56, 'kilo': Decimal('10.00')}, 23: {'product_id': 57, 'kilo': Decimal('5.00')}, 24: {'product_id': 58, 'kilo': Decimal('2.50')}}
​

total_qty_bought:

<QuerySet [{'product': 42, 'quantity__sum': Decimal('5')}, {'product': 41, 'quantity__sum': Decimal('3')}, {'product': 51, 'quantity__sum': Decimal('13')}, {'product': 50, 'quantity__sum': Decimal('34')}, {'product': 49, 'quantity__sum': Decimal('1')}, {'product': 40, 'quantity__sum': Decimal('2')}, {'product': 43, 'quantity__sum': Decimal('10')}, {'product': 52, 'quantity__sum': Decimal('12')}, {'product': 32, 'quantity__sum': Decimal('5')}, {'product': 53, 'quantity__sum': Decimal('2')}, {'product': 38, 'quantity__sum': Decimal('1')}, {'product': 55, 'quantity__sum': Decimal('1')}, {'product': 56, 'quantity__sum': Decimal('10')}, {'product': 58, 'quantity__sum': Decimal('60')}, {'product': 44, 'quantity__sum': Decimal('16')}, {'product': 57, 'quantity__sum': Decimal('28')}]>

【问题讨论】:

    标签: python django list dictionary


    【解决方案1】:
        product_data = {}
        for product in ProductSpy.objects.all():
            product_data[product.product_id] = product.kilo
       
        # Get quantity bought of each product
        total_qty_bought = self.order_item_model.objects.values("product").annotate(Sum("quantity"))
    
        our_total = {}
        for qty in total_qty_bought:
            product_id = qty['product']
            quantity = int(qty["quantity__sum"])
            try:
                kilos = quantity * product_data[product_id]
                our_total[product_id] = kilos
                print(f"Product_id: {product_id} - Kilos: {kilos}")
            except KeyError:
                print(f"Product_id {product_id} not found in product_data!")
    

    这是解决方案。

    【讨论】:

      猜你喜欢
      • 2019-02-24
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 2021-08-07
      • 2017-06-13
      • 2021-06-24
      • 2018-04-29
      • 1970-01-01
      相关资源
      最近更新 更多