【发布时间】:2013-02-13 00:51:52
【问题描述】:
我想在 Python 中创建一个加权函数。但是权重的数量会有所不同,我需要该函数具有可选参数(例如,您可以找到 weightA 和 weightB 的成本,但您也可以找到以上所有参数。
基本功能如下:
weightA = 1
weightB = 0.5
weightC = 0.33
weightD = 2
cost = 70
volumeA = 100
volumeB = 20
volumeC = 10
volumeD = 5
def weightingfun (cost, weightA, weightB, volumeA, volumeB):
costvolume = ((cost*(weightA+weightB))/(weightA*volumeA+weightB*volumeB))
return costvolume
我怎样才能改变功能,例如,我也可以称量体积 C 和体积 D?
先谢谢了!
【问题讨论】:
-
我使用了@Andrew Alcock 函数。我相信其他的也很棒,但这对我来说似乎是最简单的方法。