Dada una lista de números, la tarea es encontrar el promedio de esa lista. El promedio es la suma de elementos dividida por el número de elementos.
carga de javascript
Input : [4, 5, 1, 2] Output : 3 Explanation : Sum of the elements is 4+5+1+2 = 12 and total number of elements is 4. So average is 12/4 = 3 Input : [15, 9, 55] Output : 26.33 Explanation : Sum of the elements is 15+9+53 = 77 and total number of elements is 3. So average is 77/3 = 26.33>
Promedio de una lista usando sum() y len() en Python
En Pitón, podemos encontrar el promedio de una lista simplemente usando las funciones sum() y len().
- suma() : Usando la función sum() podemos obtener la suma de la lista.
- solo() : la función len() se utiliza para obtener la longitud o el número de elementos en una lista.
# Python program to get average of a list def Average(lst): return sum(lst) / len(lst) # Driver Code lst = [15, 9, 55, 41, 35, 20, 62, 49] average = Average(lst) # Printing average of the list print('Average of the list =', round(average, 2))>
Producción:
Average of the list = 35.75>
Complejidad del tiempo: O(n) donde n es la longitud de la lista.
Espacio auxiliar: O(1) ya que solo necesitamos una única variable para almacenar el promedio.
Promedio de una lista usando reduce() y lambda en Python
Podemos usar el reducir() para reducir el bucle y utilizando el función lambda puede calcular la suma de la lista. Usamos len() para calcular la longitud como se discutió anteriormente.
Python3
# Python program to get average of a list # Using reduce() and lambda # importing reduce() from functools import reduce def Average(lst): return reduce(lambda a, b: a + b, lst) / len(lst) # Driver Code lst = [15, 9, 55, 41, 35, 20, 62, 49] average = Average(lst) # Printing average of the list print('Average of the list =', round(average, 2))>
Producción:
Average of the list = 35.75>
Complejidad del tiempo: O(n), donde n es la longitud de la lista.
Espacio auxiliar: O(1). El espacio utilizado es constante e independiente del tamaño de la lista de entrada.
Promedio de una lista usando Python mean()
La función incorporada significar() se puede utilizar para calcular la media (promedio) de la lista.
Python3
# Python program to get average of a list # Using mean() # importing mean() from statistics import mean def Average(lst): return mean(lst) # Driver Code lst = [15, 9, 55, 41, 35, 20, 62, 49] average = Average(lst) # Printing average of the list print('Average of the list =', round(average, 2))>
Producción:
Average of the list = 35.75>
Complejidad del tiempo: O(n), donde n es la longitud de la lista.
Espacio auxiliar: O(1).
Promedio de una Lista iterando Lista en Python
Iterando liza usando el bucle for y realizando operaciones en cada elemento de la lista.
Python3 # Python code to get average of list def Average(lst): sum_of_list = 0 for i in range(len(lst)): sum_of_list += lst[i] average = sum_of_list/len(lst) return average # Driver Code lst = [15, 9, 55, 41, 35, 20, 62, 49] average = Average(lst) print('Average of the list =', round(average, 2))>
Producción:
Average of the list = 35.75>
Complejidad del tiempo: En)
Espacio auxiliar: O(n), donde n es la longitud de la lista.
Promedio de una lista usando la función numpy.average() de Python
Podemos encontrar el promedio de una lista en Python usando la función promedio() de módulo numérico .
Python3 # importing numpy module import numpy # function for finding average def Average(lst): # average function avg = numpy.average(lst) return(avg) # input list lst = [15, 9, 55, 41, 35, 20, 62, 49] # function call print('Average of the list =', round(Average(lst), 2))>
Producción:
Average of the list = 35.75>