|
|
|
|
 每60行求平均 - happyhappy [ 2005-12-03 19:51 | 382 byte(s)]
 Re: 每60行求平均 - happyhappy [ 2005-12-12 09:47 | 2,362 byte(s)]
 Re: 每60行求平均 - passworld [ 2005-12-03 21:29 | 26 byte(s)]
 Re: 每60行求平均 - passworld [ 2005-12-03 21:26 | 18 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
.091
.09
.095
.09
.09
.092
.092
.092
.087
.09
.09
.092
.093
.092
.089
.093
........
这样一个文件,一秒种一个数据,要求每小时的平均值,本来想把整列读到列表中,再利用[0:59]求和再平均,可是总是把所有的数据都读出来,split列表也不行,怎办啊?
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
import sys,string,math
fr=open("e:/zwxdata/200509/2005_09_01.txt")
fw=open("e:/zwxdata/2out2005_09_01.txt","w")
line=0
c1=[]
c2=[]
count1=1
count2=1
sum1=0
sum2=0
avarage1=1
average2=1
for i in fr.readlines():
line=line+1
for a1 in range(1,60):
if line==a1:
line21=str.split(i)
line31=float(str(line21[4]))
c1 =[line31]
for m1 in c1:
count1+=1
sum1=sum1+m1
average1=sum1/count1
for a2 in range(60,120):
if line==a2:
line22=str.split(i)
line32=float(str(line22[4]))
c2 =[line32]
for m2 in c2:
sum2=sum2+m2
count2+=1
average2=sum2/count2
print average1
print average2
fr.close()
fw.close()
我这个文件要做第4列每隔60个数的平均值,这里只作了2个,可以运行了.但要做成函数形式出错了,帮我看看吧
import sys,string,math
fr=open("e:/zwxdata/200509/2005_09_01.txt")
fw=open("e:/zwxdata/42out2005_09_01.txt","w")
line=0
count=1
c=[]
av=0.0
sum=0.0
def average():
for i in fr.readlines():
line+=1
if line==a:
line2=str.split(i)
line3=float(str(line2[4]))
c =[line3]
for m in c:
count=count+1
sum=sum+m
av=sum/count
return av
print av
for a in range(1,60):
ss=average()
|
|
|
[Original]
[Print]
[Top]
|
|
|