logo

Znajdź średnią listę w Pythonie

Mając listę liczb, zadaniem jest znalezienie średniej z tej listy. Średnia to suma elementów podzielona przez liczbę elementów.

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>

Średnia z listy przy użyciu sum() i len() w Pythonie

W Pyton, możemy znaleźć przeciętny listy za pomocą funkcji sum() i len().



  • suma() : Używając funkcji sum() możemy uzyskać sumę listy.
  • tylko() : Funkcja len() służy do pobierania długości lub liczby elementów na liście.
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))>

Wyjście:

int na ciąg C++
Average of the list = 35.75>

Złożoność czasowa: O(n) gdzie n jest długością listy.
Przestrzeń pomocnicza: O(1) ponieważ potrzebujemy tylko jednej zmiennej do przechowywania średniej.

Średnia lista przy użyciu funkcji redukcji() i lambda w Pythonie

Możemy skorzystać z zmniejszyć() aby zredukować pętlę i używając funkcja lambda potrafi obliczyć sumę listy. Do obliczenia długości używamy funkcji len(), jak omówiono powyżej.



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

Wyjście:

Average of the list = 35.75>

Złożoność czasowa: O(n), gdzie n jest długością listy lst.
Przestrzeń pomocnicza: O(1). Zajęte miejsce jest stałe i niezależne od rozmiaru listy wejściowej.

Średnia listy przy użyciu metody Pythona mean()

Wbudowana funkcja mieć na myśli() można wykorzystać do obliczenia średniej (średniej) z listy.



Klawiatura w dół
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))>

Wyjście:

Average of the list = 35.75>

Złożoność czasowa: O(n), gdzie n jest długością listy.
Przestrzeń pomocnicza: O(1).

Średnia lista poprzez iterację listy w Pythonie

Iterowanie listy użycie pętli for i wykonanie operacji na każdym elemencie listy.

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

Wyjście:

Average of the list = 35.75>

Złożoność czasowa: NA)
Przestrzeń pomocnicza: O(n), gdzie n jest długością listy.

Zmień kolor w Gimpie

Średnia z listy przy użyciu funkcji Pythona numpy.average().

Możemy znaleźć przeciętny listy w Pythonie za pomocą funkcji Average(). Moduł 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))>

Wyjście:

Average of the list = 35.75>