logo

Najděte průměr seznamu v pythonu

Vzhledem k seznamu čísel je úkolem najít průměr tohoto seznamu. Průměr je součet prvků dělený počtem prvků.

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>

Průměr seznamu pomocí sum() a len() v Pythonu

v Krajta, můžeme najít průměrný seznamu jednoduše pomocí funkcí sum() a len().



  • součet() : Pomocí funkce sum() můžeme získat součet seznamu.
  • jen() : funkce len() se používá k získání délky nebo počtu prvků v seznamu.
Python3
# 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))>

Výstup:

Average of the list = 35.75>

Časová složitost: O(n) kde n je délka seznamu.
Pomocný prostor: O(1) protože pro uložení průměru vyžadujeme pouze jednu proměnnou.

Průměr seznamu pomocí reduction() a lambda v Pythonu

Můžeme použít snížit() snížit smyčku a pomocí funkce lambda umí vypočítat součet seznamu. K výpočtu délky používáme len(), jak je uvedeno výše.



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))>

Výstup:

Average of the list = 35.75>

Časová složitost: O(n), kde n je délka seznamu lst.
Pomocný prostor: O(1). Použitý prostor je konstantní a nezávislý na velikosti vstupního seznamu.

Průměr seznamu pomocí Pythonu mean()

Vestavěná funkce znamenat() lze použít k výpočtu průměru (průměru) seznamu.



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))>

Výstup:

Average of the list = 35.75>

Časová složitost: O(n), kde n je délka seznamu.
Pomocný prostor: O(1).

Průměr seznamu iterací seznamu v Pythonu

Iterování seznamy pomocí cyklu for a provádění operací s každým prvkem seznamu.

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))>

Výstup:

Average of the list = 35.75>

Časová složitost: Na)
Pomocný prostor: O(n), kde n je délka seznamu.

Průměr seznamu pomocí funkce numpy.average() Pythonu

Můžeme najít průměrný seznamu v Pythonu pomocí funkce average() of Modul NumPy .

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))>

Výstup:

Average of the list = 35.75>