【问题标题】:How to calculate shipping costs for a multi-vendor in Python?如何在 Python 中计算多供应商的运费?
【发布时间】:2021-05-03 08:12:48
【问题描述】:

如何在 Python 中计算多供应商的运费?

这是我购物车中的数据:

cart =  {"line1" : {'vendor1': 1.2,'shipping_cost': 10000,'city': "Manchester",},
         "line2" :  {'vendor2': 2,'shipping_cost': 20000,'city': "Liverpool",},
         "line3" : {'vendor1': 1.2,'shipping_cost': 10000,'city': "Manchester",},
         "line4" :  {'vendor2': 3,'shipping_cost': 20000,'city': "Liverpool",},
}

我想按供应商计算运费如下:

(vendor1 + vendor1) *  shipping_cost = 2.4 * 10.000 = 24.000
(vendor2 + vendor2) *  shipping_cost = 5 * 20.000 = 100.000

如何在python中实现这个计算

【问题讨论】:

    标签: python-3.x dictionary calculation


    【解决方案1】:

    您可以创建一个字典来存储成本。 您可以获取此字典的供应商键并分配该值的总成本。

    cart =  {"line1" : {'vendor1': 1.2,'shipping_cost': 10000,'city': "Manchester",},
             "line2" :  {'vendor2': 2,'shipping_cost': 20000,'city': "Liverpool",},
             "line3" : {'vendor1': 1.2,'shipping_cost': 10000,'city': "Manchester",},
             "line4" :  {'vendor2': 3,'shipping_cost': 20000,'city': "Liverpool",},
    }
    
    cost = {}
    for sale in cart.values():
        vendor = [key for key in sale.keys() if 'vendor' in key][0]
        if vendor in cost.keys():
            cost[vendor] += sale['shipping_cost']*sale[vendor]
        else:
            cost[vendor] = sale['shipping_cost']*sale[vendor]
    print(cost)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      • 2020-04-20
      • 2011-11-27
      • 2016-05-09
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多