【问题标题】:How can I create a Python function with variable arguments [closed]如何创建具有可变参数的 Python 函数 [关闭]
【发布时间】:2013-02-13 00:51:52
【问题描述】:

我想在 Python 中创建一个加权函数。但是权重的数量会有所不同,我需要该函数具有可选参数(例如,您可以找到 weightAweightB 的成本,但您也可以找到以上所有参数。

基本功能如下:

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 函数。我相信其他的也很棒,但这对我来说似乎是最简单的方法。

标签: python function weighting


【解决方案1】:

这可以通过对元组或列表的操作非常简单地完成:

import operator
def weightingfun(cost, weights, volumes):
    return cost*sum(weights)/sum(map( operator.mul, weights, volumes))

weights = (1, 0.5, 0.33, 2)
volumes = (100, 20, 10, 5)
print weightingfun(70, weights, volumes)

【讨论】:

  • 非常感谢您的帮助!这非常有效。
【解决方案2】:

最好使用具有重量/体积属性的对象(劳伦斯的帖子)

但要展示如何压缩两个元组:

weights = (1, 0.5, 0.33, 2)
volumes = (100, 20, 10, 5)

def weightingfun(cost, weights, volumes):
    for w,v in zip(weights, volumes):
            print "weight={}, volume={}".format(w, v)

weightingfun(70, weights, volumes)

【讨论】:

    【解决方案3】:

    两个选项:一个使用两个列表:

         # option a:
         # make two lists same number of elements
         wt_list=[1,0.5,0.33,2]
         vol_list=[100,20,10,5]
    
         cost = 70
    
         def weightingfun (p_cost, p_lst, v_lst):
              a = p_cost * sum(p_lst)
             sub_wt   = 0
             for i in range(0,len(v_lst)):
                 sub_wt = sub_wt + (p_lst[i] *  v_lst[i])
             costvolume = a/sub_wt
            return costvolume
    
         print weightingfun (cost, wt_list, vol_list)
    

    第二种选择是使用字典

    【讨论】:

      【解决方案4】:

      其实也有这个方法,最后和传一个列表是一样的,

      def weightingfun2(cost, *args):
          for arg in args:
             print "variable arg:", arg
      
      if __name__ == '__main__':
           weightingfun2(1,2,3,"asdfa")
      

      要了解真正发生的事情的详细信息,您可以到达那里: http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/

      【讨论】:

        【解决方案5】:

        替换weightAweightBvolumeAvolumeB参数将收集参数。例如:

        • 重量列表和体积列表
        • (重量、体积)元组的列表/集合
        • 具有重量和体积属性的对象列表/集合

        最后一个例子:

        def weightingfun(cost, objects):
            totalWeight = sum((o.weight for o in objects))
            totalWeightTimesVolume = sum(((o.weight * o.volume) for o in objects))
            costvolume = (cost*totalWeight)/totalWeightTimesVolume
            return costvolume
        

        【讨论】:

        • 我也是这么想的。然而,重量和体积的数量会不时变化。此外,如何才能将正确的重量与正确的体积联系起来(即 weightA*volumeA)?
        • 哦,看起来不错。但是,我如何创建对象列表/集合?抱歉,我对 Python 很陌生。
        • 对列表使用[a, b, c],对元组使用(a, b, c)
        猜你喜欢
        • 2019-03-01
        • 1970-01-01
        • 2022-06-15
        • 1970-01-01
        • 1970-01-01
        • 2018-07-25
        • 2012-03-21
        • 2013-07-17
        相关资源
        最近更新 更多