Kumar Raman
Kumar Raman

Reputation: 11

python - How to count and take averages on a tuple with Paired values

I am new to python. Can you please help me with this? I have a tuple lets say (100, (10.0, 20.0, 30.0)). I need to write a function which will take this as an input and return (100, (3, 20.0)) where 3 is the value count and 20 is the value of average.

Upvotes: 0

Views: 104

Answers (3)

Kumar Raman
Kumar Raman

Reputation: 11

Thanks guys for all the inputs! I was able to build the function using the information you guys have provided.

def getCountAndAverages(a):
CountAndAverage=[]
for i in range(0,(len(a))):
    if (i%2)!=0:
        summation=sum(list(a[i]))
        count=len(a[i])
        average=summation/count
        CountAndAverage.append((count,average))
    else:
        CountAndAverage.append(a[i])
mytuple=tuple(CountAndAverage)
return(mytuple)

Upvotes: 0

user4453877
user4453877

Reputation:

Have you tried something like this with numpy:

import numpy
def myfunction(mytuple):
    myresult=(mytuple[0],(len(mytuple[1]),numpy.mean(mytuple[1])))
    return myresult

Upvotes: 1

Utsav T
Utsav T

Reputation: 1545

Try this for a starter code. :

def function(tup):

    f = tuple()
    a = len(tup[1])        ## Finds the length of the list of numbers
    b = sum(list(tup[1]))  ## Converts the second item of the tuple into a list and finds the sum

    f = ( tup[0] , (a, b/float(a)) ) ## The final result

    return f

Upvotes: 0

Related Questions