Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to condense this function?

Tags:

python

def average(tup):
  """ ugiufh """
   total = ((int(tup[0]) + int(tup[1]) + int(tup[2]))/3,
            (int(tup[0]) + int(tup[1]) + int(tup[2]))/3,
            (int(tup[0]) + int(tup[1]) + int(tup[2]))/3)
   return total

I am writing a function to average out three element in a tuple which means if the original tuple = (1, 2, 3) which give me tuple = (2, 2, 2)

My question is there any way to condense what wrote to give me the same answer? If yes, how to condense it?

Thanks

like image 582
hkus10 Avatar asked May 29 '26 23:05

hkus10


1 Answers

If you are sure you want integer division, you can use

def average(tup):
    n = len(tup)
    return (sum(int(x) for x in tup) / n,) * n
like image 84
Sven Marnach Avatar answered May 31 '26 15:05

Sven Marnach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!