-1

I want to create a weighting function in Python. However the amount of weighting varies and I need the function to have optional parameters (for instance, you could find the cost of weightA and weightB, but you could also find all of the above.

The basic function looks like this:

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

How can I change the function so that I could for example also weight volume C and volume D?

Thanks ahead!

xxmbabanexx
  • 8,256
  • 16
  • 40
  • 60
ustroetz
  • 5,802
  • 16
  • 47
  • 74
  • I used @Andrew Alcock function. I am sure the other ones are great as well, but this just seemed the easiest way to me. – ustroetz Feb 13 '13 at 17:52

5 Answers5

0

Replace the weightA, weightB, volumeA, volumeB parameters will collection parameters. For example:

  • a list of weights and a list of volumes
  • a list/set of (weight,volume) tuples
  • a list/set of objects with weight and volume attributes

An example of the last one:

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
Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
  • I was thinking the same. However the quantity of weights and volumes varies from time to time. Also how would it then be possible to connect the correct weight to the correct volume (i.e. weightA*volumeA)? – ustroetz Feb 13 '13 at 01:00
  • Oh that looks good. However how could I create the list/set of objects? Sorry, I am quite new to Python. – ustroetz Feb 13 '13 at 01:11
  • Use `[a, b, c]` for lists, `(a, b, c)` for tuples. – Dominic K Feb 13 '13 at 01:13
0

You're better off using the object with weight/volume properties (Laurence's post)

But to show how to zip two tuples:

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)
ninMonkey
  • 7,211
  • 8
  • 37
  • 66
0

Two options: a use two lists:

     # 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)

second option is to use a dictionary

TomJones
  • 51
  • 2
0

This can be done very simply with operations on tuples or lists:

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)
Andrew Alcock
  • 19,401
  • 4
  • 42
  • 60
0

In fact there is also this method, in the end it's the same thing as passing a list,

def weightingfun2(cost, *args):
    for arg in args:
       print "variable arg:", arg

if __name__ == '__main__':
     weightingfun2(1,2,3,"asdfa")

To get the detail of what really happening you can get there: http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/

J M
  • 31
  • 1
  • 4