파이썬 평균 구하기

1 개요[ | ]

파이썬 평균 구하기
파이썬 average()
Python 3
nums = [1, 2, 3, 4]
print( sum(nums)/len(nums) ) # 2.5
nums = [1, 2, 3, 4, 5]
print( sum(nums)/len(nums) ) # 3.0
Python 2
nums = [1, 2, 3, 4]
print( sum(nums)/len(nums) ) # 2
nums = [1, 2, 3, 4, 5]
print( sum(nums)/len(nums) ) # 3

nums = [1, 2, 3, 4]
print( 1.0*sum(nums)/len(nums) ) # 2.5
nums = [1, 2, 3, 4, 5]
print( 1.0*sum(nums)/len(nums) ) # 3.0

2 NumPy[ | ]

NumPy
import numpy as np
a = np.array([1,2,3,4])
print( a.mean() ) # 2.5

3 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}