Python에서 목록의 평균을 찾는 5가지 방법
안녕 여러분! 이 기사에서는 Python 목록에서 목록의 평균을 찾는 다양한 방법을 살펴보겠습니다.
일반적으로 평균은 전체 데이터 항목 또는 요소 집합을 나타내는 값입니다.
공식: 평균 = 숫자의 합/총 개수.
Python에서 목록의 평균을 찾는 기술
다음 기술 중 하나를 사용하여 Python에서 목록의 평균/평균을 계산할 수 있습니다.
- 파이썬 mean() 함수
- 내장 sum() 메서드
- Python 람다 및 reduce() 메서드
- 파이썬 operator.add() 메서드
1. 파이썬 mean() 함수
Python 3에는 숫자의 평균을 계산하는 내장 함수가 포함된 통계 모듈
이 있습니다. 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() 함수를 사용할 수 있습니다.
Python reduce() 함수: reduce() 함수
는 기본적으로 함수에 전달된 요소 집합에 특정(입력) 함수를 적용하는 데 사용됩니다.
통사론:
reduce(function,input-list/sequence)
- 처음에 reduce() 함수는 전달된 함수를 처음 두 개의 연속 요소에 적용하고 결과를 반환합니다.
- 다음으로, 이전 단계에서 얻은 결과와 두 번째 요소를 잇는 요소에 동일한 기능을 적용합니다.
- 이 프로세스는 목록 끝에 도달할 때까지 계속됩니다.
- 마지막으로 결과는 출력으로 터미널/화면에 반환됩니다.
익명 함수, 즉 이름이나 서명이 없는 함수.
통사론:
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.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의 평균을 찾는 다양한 기술을 공개하고 이해했습니다.
참조
- NumPy average() 메서드 - 공식 문서
- 연산자 모듈 - 공식 문서
- 파이썬 NumPy 모듈
- 파이썬 목록