"파이썬 평균 구하기"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
3번째 줄: 3번째 줄:
;파이썬 average()
;파이썬 average()
{{소스헤더|Python 3}}
{{소스헤더|Python 3}}
<source lang='python' run>
<syntaxhighlight lang='python' run>
nums = [1, 2, 3, 4]
nums = [1, 2, 3, 4]
print( sum(nums)/len(nums) ) # 2.5
print( sum(nums)/len(nums) ) # 2.5
</source>
</syntaxhighlight>
<source lang='python' run>
<syntaxhighlight lang='python' run>
nums = [1, 2, 3, 4, 5]
nums = [1, 2, 3, 4, 5]
print( sum(nums)/len(nums) ) # 3.0
print( sum(nums)/len(nums) ) # 3.0
</source>
</syntaxhighlight>


{{소스헤더|Python 2}}
{{소스헤더|Python 2}}
<source lang='python'>
<syntaxhighlight lang='python'>
nums = [1, 2, 3, 4]
nums = [1, 2, 3, 4]
print( sum(nums)/len(nums) ) # 2
print( sum(nums)/len(nums) ) # 2
23번째 줄: 23번째 줄:
nums = [1, 2, 3, 4, 5]
nums = [1, 2, 3, 4, 5]
print( 1.0*sum(nums)/len(nums) ) # 3.0
print( 1.0*sum(nums)/len(nums) ) # 3.0
</source>
</syntaxhighlight>


==NumPy==
==NumPy==
{{참고|NumPy mean()}}
{{참고|NumPy mean()}}
{{소스헤더|NumPy}}
{{소스헤더|NumPy}}
<source lang='python' run>
<syntaxhighlight lang='python' run>
import numpy as np
import numpy as np
a = np.array([1,2,3,4])
a = np.array([1,2,3,4])
print( a.mean() ) # 2.5
print( a.mean() ) # 2.5
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:32 판

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 }}