Având în vedere o listă de numere, sarcina este de a găsi media acelei liste. Media este suma elementelor împărțită la numărul de elemente.
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>
Media unei liste folosind sum() și len() în Python
În Piton, putem găsi in medie a unei liste prin simpla folosire a funcțiilor sum() și len().
- sumă() : Folosind funcția sum() putem obține suma listei.
- numai() : funcția len() este folosită pentru a obține lungimea sau numărul de elemente dintr-o listă.
# 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))>
Ieșire:
java citiți csv
Average of the list = 35.75>
Complexitatea timpului: O(n) unde n este lungimea listei.
Spațiu auxiliar: O(1) deoarece avem nevoie doar de o singură variabilă pentru a stoca media.
Media unei liste folosind reduce() și lambda în Python
Putem folosi reduce() pentru a reduce bucla și prin utilizarea functie lambda poate calcula însumarea listei. Folosim len() pentru a calcula lungimea așa cum sa discutat mai sus.
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))>
Ieșire:
Average of the list = 35.75>
Complexitatea timpului: O(n), unde n este lungimea listei lst.
Spatiu auxiliar: O(1). Spațiul folosit este constant și independent de dimensiunea listei de intrare.
Media unei liste folosind Python mean()
Funcția încorporată Rău() poate fi folosit pentru a calcula media (media) a listei.
citiți din fișierul csv în 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))>
Ieșire:
Average of the list = 35.75>
Complexitatea timpului: O(n), unde n este lungimea listei.
Spatiu auxiliar: O(1).
Media unei liste prin iterarea Listei în Python
Repetând liste folosind for loop și efectuând operații pe fiecare element al listei.
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))>
Ieșire:
Average of the list = 35.75>
Complexitatea timpului: Pe)
Spatiu auxiliar: O(n), unde n este lungimea listei.
șir în java boolean
Media unei liste folosind funcția Python numpy.average().
Putem găsi in medie a unei liste în Python utilizând funcția average() of Modulul 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))>
Ieșire:
Average of the list = 35.75>