logo

Trobeu la mitjana d'una llista a Python

Donada una llista de números, la tasca és trobar la mitjana d'aquesta llista. La mitjana és la suma d'elements dividida pel nombre d'elements.

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>

Mitjana d'una llista utilitzant sum() i len() a Python

En Python, podem trobar el mitjana d'una llista utilitzant simplement les funcions sum() i len().



  • suma() : Utilitzant la funció sum() podem obtenir la suma de la llista.
  • només () : la funció len() s'utilitza per obtenir la longitud o el nombre d'elements d'una llista.
Python 3
# 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))>

Sortida:

Average of the list = 35.75>

Complexitat temporal: O(n) on n és la longitud de la llista.
Espai auxiliar: O(1) ja que només necessitem una única variable per emmagatzemar la mitjana.

Mitjana d'una llista utilitzant reduce() i lambda a Python

Podem utilitzar el reduir () per reduir el bucle i utilitzant el funció lambda pot calcular la suma de la llista. Utilitzem len() per calcular la longitud tal com s'ha comentat anteriorment.



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

Sortida:

làtex de mida de lletra
Average of the list = 35.75>

Complexitat temporal: O(n), on n és la longitud de la llista lst.
Espai auxiliar: O(1). L'espai utilitzat és constant i independent de la mida de la llista d'entrada.

Mitjana d'una llista amb Python mean()

La funció integrada significar() es pot utilitzar per calcular la mitjana (mitjana) de la llista.



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

Sortida:

Average of the list = 35.75>

Complexitat temporal: O(n), on n és la longitud de la llista.
Espai auxiliar: O(1).

Mitjana d'una llista iterant la llista en Python

Iterant llistes utilitzant el bucle for i fent operacions a cada element de la llista.

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

Sortida:

Average of the list = 35.75>

Complexitat temporal: O(n)
Espai auxiliar: O(n), on n és la longitud de la llista.

Mitjana d'una llista utilitzant la funció numpy.average() de Python

Podem trobar el mitjana d'una llista en Python utilitzant la funció average() de Mòdul NumPy .

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

Sortida:

Average of the list = 35.75>