Average value of lookup list

Posted by ksb24930 on Fri, 31 Jan 2020 06:34:55 +0100

I must be Python The average value of the list found in. So far, this is my code

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print reduce(lambda x, y: x + y, l)

I already know, so it can add the values in the list, but I don't know how to divide it into them?

#1 building

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

l = map(float,l)
print '%.2f' %(sum(l)/len(l))

#2 building

Statistics Module already Add to python 3.4 . It has the function of calculating the average value, which is called mean value . An example of the list you provide is:

from statistics import mean
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
mean(l)

#3 building

In addition to casting it to a floating-point number, you can add 0.0 to the sum:

def avg(l):
    return sum(l, 0.0) / len(l)

#4 building

I have similar problems to solve in Udacity. I'm not coding built-in functions:

def list_mean(n):

    summing = float(sum(n))
    count = float(len(n))
    if n == []:
        return False
    return float(summing/count)

Longer than usual, but for beginners, it's a big challenge.

#5 building

Both can give you similar values close to integers or at least 10 decimal values. However, if you really think about long floating-point values, the two may be different. The approach may vary depending on what you are trying to achieve.

>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> print reduce(lambda x, y: x + y, l) / len(l)
20
>>> sum(l)/len(l)
20

Floating value

>>> print reduce(lambda x, y: x + y, l) / float(len(l))
20.1111111111
>>> print sum(l)/float(len(l))
20.1111111111

@Andrew Clark was right.

#6 building

I tried to use the options above, but it didn't work. Try this:

from statistics import mean

n = [11, 13, 15, 17, 19]
print(n)
print(mean(n))

Working on python 3.5

#7 building

In combination with the above answers, I have come to the following for use with reduce, and do not assume that you have L available in the reduce function:

from operator import truediv

L = [15, 18, 2, 36, 12, 78, 5, 6, 9]

def sum_and_count(x, y):
    try:
        return (x[0] + y, x[1] + 1)
    except TypeError:
        return (x + y, 2)

truediv(*reduce(sum_and_count, L))

# prints 
20.11111111111111

#8 building

As a beginner, I just code like this:

L = [15, 18, 2, 36, 12, 78, 5, 6, 9]

total = 0

def average(numbers):
    total = sum(numbers)
    total = float(total)
    return total / len(numbers)

print average(L)

#9 building

I want to add another way

import itertools,operator
list(itertools.accumulate(l,operator.add)).pop(-1) / len(l)

#10 building

numbers = [0,1,2,3]

numbers[0] = input("Please enter a number")

numbers[1] = input("Please enter a second number")

numbers[2] = input("Please enter a third number")

numbers[3] = input("Please enter a fourth number")

print (numbers)

print ("Finding the Avarage")

avarage = int(numbers[0]) + int(numbers[1]) + int(numbers[2]) + int(numbers [3]) / 4

print (avarage)

#11 building

hypothesis

x = [[-5.01,-5.43,1.08,0.86,-2.67,4.94,-2.51,-2.25,5.56,1.03], [-8.12,-3.48,-5.52,-3.78,0.63,3.29,2.09,-2.13,2.86,-3.33], [-3.68,-3.54,1.66,-4.11,7.39,2.08,-2.59,-6.94,-2.26,4.33]]

You can notice that the x dimension is 3 * 10. If you need to get the mean of each line, you can enter

theMean = np.mean(x1,axis=1)

Don't forget to import numpy as np

#12 building

If you are using Python > = 3.4, there is a statistical database

https://docs.python.org/3/library/statistics.html

You can use this despicable method. Suppose you have a list of numbers to find the mean:-

list = [11, 13, 12, 15, 17]
import statistics as s
s.mean(list)

It also has other methods, such as stdev, variance, mode, harmonic mean, median and so on. These methods are also very useful.

#13 building

If you want to get more than just an average, you can view the scipy statistics

from scipy import stats
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(stats.describe(l))

# DescribeResult(nobs=9, minmax=(2, 78), mean=20.11111111111111, 
# variance=572.3611111111111, skewness=1.7791785448425341, 
# kurtosis=1.9422716419666397)

#14 building

Or use pandas's Series.mean method:

pd.Series(sequence).mean()

Demonstration:

>>> import pandas as pd
>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> pd.Series(l).mean()
20.11111111111111
>>> 

From the document:

Series.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs) ΒΆ

This is the document:

https://pandas.pydata.org/pandas-docs/stable/generation/pandas.Series.mean.html

And the entire document:

https://pandas.pydata.org/pandas-docs/stable/10min.html

#15 building

Use the following PYTHON code to find the average in the list:

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(sum(l)//len(l))

It's easy to try this.

#16 building

On Python 3.4 +, you can use the statistics.mean()

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

import statistics
statistics.mean(l)  # 20.11111111111111

On older versions of Python, you can

sum(l) / len(l)

On Python 2, you need to convert len to float to get float Division

sum(l) / float(len(l))

There is no need to use reduce. It's much slower, and already In Python 3 delete .

#17 building

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
sum(l) / len(l)

#18 building

When Python has the perfect crossover sum() function, why use reduce() for this?

print sum(l) / float(len(l))

(float() is required to force Python to perform floating-point division.)

#19 building

print reduce(lambda x, y: x + y, l)/(len(l)*1.0)

Or like previous releases

sum(l)/(len(l)*1.0)

1.0 is to ensure that floating-point division is obtained

#20 building

In order to use reduce to get the running average, you need to track the total number, but also the total number of elements you have seen so far. Since this is not a trivial element in the list, you must also pass reduce to collapse into an additional parameter.

>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> running_average = reduce(lambda aggr, elem: (aggr[0] + elem, aggr[1]+1), l, (0.0,0))
>>> running_average[0]
(181.0, 9)
>>> running_average[0]/running_average[1]
20.111111111111111

#21 building

sum(l) / float(len(l)) is the correct answer, but for completeness only, you can use a reduce to calculate the average:

>>> reduce(lambda x, y: x + y / float(len(l)), l, 0)
20.111111111111114

Note that this can result in minor rounding errors:

>>> sum(l) / float(len(l))
20.111111111111111

#22 building

You can use the numpy.mean :

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

import numpy as np
print(np.mean(l))
Published 0 original articles, won praise 2, visited 8036
Private letter follow

Topics: Python Lambda Database