當前位置:首頁 » 編程語言 » python平均數

python平均數

發布時間: 2022-01-09 22:33:03

python求平均數問題

#-*-coding:utf-8-*-
f=open("1.txt")
s=f.read()
f.close()
ls=s.split(" ")
s=""
forlinls:
ss=l.split(" ")
le=len(ss)
ifle>2:
sum=0
foriinrange(2,le):
sum+=int(ss[i])
print1.0*sum/(le-2),' ',le-2
s+=ss[0]+" "+ss[1]+" "+str(1.0*sum/(le-2))+" "+str(le-2)+" "

f=open("2.txt","w")
f.write(s)
f.close()

運行結果:

41.75 4

70.6666666667 3

35.5714285714 7

cat 2.txt

廣州 guangzhou 41.75 4

廣州 guangzhouone 70.6666666667 3

深圳 shenzhen 35.5714285714 7

❷ 平均值python

平均值有算術平均值,幾何平均值,平方平均值(均方根平均值,rms),調和平均值,加權平均值等,其中以算術平均值最為常見。分類平均值,有算術平均值,幾何平均值,平方平均值(均方根平均值,rms),調和平均值,加權平均值等。

❸ python裡面求平均數

arr=[]
whileTrue:
tmp=input('enteranumber>>')
i=int(tmp)
ifi>=0:
arr.append(i)
else:
break
l=len(arr)
y=0
foriinrange(0,l):
y=y+arr[i]
print'theaverageofthenumbersis',1.0*y/l

❹ python怎麼求列表的平均值

當列表list中只包含數字時,如何求取它的平均數:
from numpy import *
a = [52,69,35,65,89,15,34]
b = mean(a)
print(b)1234

運行結果:
51.285714285714285

❺ 用Python怎麼求一組數的平均值

>>>l=[1,2,3,4,5,4,3,2,1]#輸入數字到數組中
>>>sum(l)/len(l)#求平均數
2.7777777777777777
>>>"{:.3f}".format(sum(l)/len(l))#求平均數,保留3位小數
'2.778'

❻ python編程輸入n.求平均數

「python編程輸入n.求平均數」

==>

輸入n個正整數,計算其平均值

==>

defenterInteger(prompt="Enteraninteger:"):
"""promptinputaninteger"""
while1:
try:
returnint(raw_input(prompt))
except:
pass

defmain(n):
data=[enterInteger()forxinxrange(n)]
ifdata:
print"Avg:%.2f"%(1.*sum(data)/len(data))

if__name__=="__main__":
main(5)

❼ 求一個python程序,計算三個數的平均數和方差

def fangcha():
a=float(raw_input("請輸入a:"))
b=float(raw_input("請輸入b:"))
c=float(raw_input("請輸入C:"))
d=(a+b+c)/3.0
e=((a-d)**2+(b-d)**2+(c-d)**2)/3.0
print "平均數是:%f方差是:%f" %(d,e)
fangcha()
Python2.7可用

❽ python 3 求平均值

❾ Python求平均數

你可以在第三行之前加一行 print(type(a),type(b)) 輸出會是 <class 'str'>,<class 'str'>
而字元串是不能除以2的。所以你要把input()強轉一下,如 a = int(input("請輸入第一個數:"))

❿ Python求平均數大學計算機題

defexpn():
n=int(input("請輸入1-9的任一數字:"))
m=n
s=2
ifninrange(1,10):
whilen-1:
n-=1
s*=2
print("2^%d=%d"%(m,s))
else:
print("InPutERROR")


expn()

代碼如上:執行結果如下:

熱點內容
聯想存儲機櫃 發布:2024-09-08 11:37:38 瀏覽:679
安卓電腦和蘋果系統哪個好 發布:2024-09-08 11:35:59 瀏覽:860
改進bp演算法 發布:2024-09-08 11:22:23 瀏覽:977
酷狗怎麼清除緩存 發布:2024-09-08 11:17:29 瀏覽:155
開發板主板交叉編譯 發布:2024-09-08 11:12:59 瀏覽:167
手機學c語言軟體 發布:2024-09-08 11:12:03 瀏覽:281
java培訓課程有那些 發布:2024-09-08 11:11:30 瀏覽:652
舊筆記本如何裝安卓系統 發布:2024-09-08 11:10:20 瀏覽:953
安卓怎麼關閉藍牙自動連接 發布:2024-09-08 10:58:12 瀏覽:11
tsm伺服器修改ip地址 發布:2024-09-08 10:21:06 瀏覽:615