파이썬에서 리스트의 평균을 찾는 5가지 방법

안녕하세요 여러분! 이 기사에서는 Python List에서 목록의 평균을 찾는 방법을 살펴보겠습니다.

일반적으로 평균은 전체 데이터 항목이나 요소를 대표하는 값입니다.

공식: 평균 = 숫자의 합 / 총 개수.


Python에서 목록의 평균을 찾는 기술

다음 기술 중 하나를 사용하여 Python에서 목록의 평균을 계산할 수 있습니다:

  • Python mean() 함수
  • 내장 sum() 메서드
  • Python lambda와 reduce() 메서드
  • Python operator.add() 메서드

1. Python mean() 함수

Python 3에는 statistics 모듈이 있으며 숫자의 평균이나 평균을 계산하는 내장 함수가 포함되어 있습니다. statistics.mean() 함수는 입력 값 또는 데이터 세트의 평균 / 평균을 계산하는 데 사용됩니다.

mean() 함수은 숫자 값이 포함된 리스트, 튜플 또는 데이터 세트를 매개변수로 받아 데이터 항목의 평균을 반환합니다.

구문:

mean(data-set/input-values)

예시:

from statistics import mean 

inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88] 
list_avg = mean(inp_lst) 

print("Average value of the list:\n") 
print(list_avg) 
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(list_avg,3))

위의 코드 스니펫에서는 statistics.round() 메서드를 사용하여 평균 출력을 특정 소수 자릿수까지 반올림했습니다.

구문:

statistics.round(value, precision value)

출력:

Average value of the list:

67.51375
Average value of the list with precision upto 3 decimal value:

67.514

2. Python의 sum() 함수 사용

Python의 statistics.sum() 함수도 Python 리스트의 데이터 값들의 평균을 계산하는 데 사용할 수 있습니다.

statistics.len() 함수는 리스트의 길이, 즉 리스트에 있는 데이터 항목의 개수를 계산하는 데 사용됩니다.

구문:

len(input-list)

또한 statistics.sum() 함수를 사용하여 리스트의 모든 데이터 항목의 합계를 계산합니다.

구문:

sum(input-list)

참고: 평균 = (합계)/(개수).

예시:

from statistics import mean 

inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]

sum_lst = sum(inp_lst)

lst_avg = sum_lst/len(inp_lst)
print("Average value of the list:\n") 
print(lst_avg) 
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(lst_avg,3))

출력:

Average value of the list:

67.51375
Average value of the list with precision upto 3 decimal value:

67.514


3. Python reduce() 및 lambda 방법 사용

우리는 Python reduce() 함수와 lambda() 함수를 함께 사용할 수 있습니다.

Python reduce() 함수: reduce() 함수는 주어진 함수를 전달된 요소 집합에 적용하는 데 사용됩니다.

구문:

reduce(function,input-list/sequence)
  • 처음에 reduce() 함수는 전달된 함수를 첫 두 연속 요소에 적용하고 결과를 반환합니다.
  • 그런 다음, 우리는 이전 단계에서 얻은 결과와 두 번째 요소 다음에 오는 요소를 같은 함수에 적용합니다.
  • 이 과정은 리스트의 끝에 도달할 때까지 계속됩니다.
  • 마지막으로, 결과가 출력으로 터미널/화면에 반환됩니다.

Python lambda() 함수: lambda() 함수는 이름이나 서명이없는 익명 함수를 만들고 형성하는 데 사용됩니다.

구문:

lambda arguments:function

예제:

from functools import reduce 

inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]

lst_len= len(inp_lst)

lst_avg = reduce(lambda x, y: x + y, inp_lst) /lst_len 
print("Average value of the list:\n") 
print(lst_avg) 
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(lst_avg,3))

출력:

Average value of the list:

67.51375
Average value of the list with precision upto 3 decimal value:

67.514


4. 리스트의 평균을 찾는 Python operator.add() 함수

Python의 operator 모듈에는 기본적인 계산과 작업을 효율적으로 수행하기 위한 다양한 함수가 포함되어 있습니다.

operator.add() 함수는 Python reduce() 함수를 사용하여 리스트에 있는 모든 데이터 값의 합을 계산하는 데 사용할 수 있습니다.

구문:

operator.add(value1, value2)

참고: 평균 = (합계)/(요소의 길이 또는 개수)

예제:

from functools import reduce 
import operator
inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]

lst_len = len(inp_lst)

lst_avg = reduce(operator.add, inp_lst) /lst_len 
print("Average value of the list:\n") 
print(lst_avg) 
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(lst_avg,3))

출력:

Average value of the list:

67.51375
Average value of the list with precision upto 3 decimal value:

67.514

5. Python에서 리스트의 평균을 계산하는 NumPy average() 메서드

Python의 NumPy 모듈은 데이터 세트나 리스트에 있는 데이터 항목의 평균/평균을 계산하기 위한 내장 함수를 갖고 있습니다.

numpy.average() 메서드는 입력 리스트의 평균을 계산하는 데 사용됩니다.

예제:

import numpy

inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]

lst_avg = numpy.average(inp_lst)
print("Average value of the list:\n") 
print(lst_avg) 
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(lst_avg,3))

출력:

Average value of the list:

67.51375
Average value of the list with precision upto 3 decimal value:

67.514

결론

따라서, 이 글에서는 Python List의 평균을 구하는 다양한 기술들을 알아보고 이해했습니다.


참고 자료

Source:
https://www.digitalocean.com/community/tutorials/average-of-list-in-python