Vzhľadom na zoznam čísel je úlohou nájsť priemer tohto zoznamu. Priemer je súčet prvkov vydelený počtom prvkov.
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>
Priemer zoznamu pomocou sum() a len() v Pythone
In Python, môžeme nájsť priemer zoznamu jednoduchým použitím funkcií sum() a len().
- suma() : Pomocou funkcie sum() môžeme získať súčet zoznamu.
- len() : funkcia len() sa používa na získanie dĺžky alebo počtu prvkov v zozname.
# 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ýkon:
triediť pole java
Average of the list = 35.75>
Časová zložitosť: O(n) kde n je dĺžka zoznamu.
Pomocný priestor: O(1) pretože na uloženie priemeru vyžadujeme iba jednu premennú.
Priemer zoznamu pomocou reduction() a lambda v Pythone
Môžeme použiť znížiť () na zníženie slučky a pomocou lambda funkcia vie vypočítať súčet zoznamu. Na výpočet dĺžky používame len(), ako je uvedené vyššie.
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ýkon:
Average of the list = 35.75>
Časová zložitosť: O(n), kde n je dĺžka zoznamu lst.
Pomocný priestor: O(1). Použitý priestor je konštantný a nezávislý od veľkosti vstupného zoznamu.
Priemer zoznamu pomocou Pythonu mean()
Vstavaná funkcia stredný() možno použiť na výpočet priemeru (priemeru) zoznamu.
formátovať reťazec javaPython3
# 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ýkon:
Average of the list = 35.75>
Časová zložitosť: O(n), kde n je dĺžka zoznamu.
Pomocný priestor: O(1).
Priemer zoznamu iteráciou zoznamu v Pythone
Opakovanie zoznamy používanie cyklu for a vykonávanie operácií na každom prvku zoznamu.
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ýkon:
Average of the list = 35.75>
Časová zložitosť: O(n)
Pomocný priestor: O(n), kde n je dĺžka zoznamu.
mvc v jarnom rámci
Priemer zoznamu pomocou funkcie numpy.average() Pythonu
Môžeme nájsť priemer zoznamu v Pythone pomocou funkcie average(). 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ýkon:
Average of the list = 35.75>