查找时间戳的平均值

时间:2014-09-17 10:09:56

标签: python list file dictionary

我有一个包含3个cols的数据,我正在计算Col[0]Col[1]的对出现次数,并将Col[2]对应的对Col[0]和{{}的值相加1}}。我可以很好地找到这些内容,但我也在尝试计算与Col[1]Col[3]对应的Col[0]中的平均值。但是当我计算平均值时,我得到所有值的平均值,并且相同值显示为永久值。任何建议我应该改变什么?

输入文件:

Col[1]

代码:

3545 3140 51.0
4602 183 2280.0
3545 3140 16.0
4945 3545 333.0
4945 3545 274.0
4391 2814 16.0
4945 3545 386.0
5045 4921 63078.0
5045 3545 896.0
4921 3545 896.0
5045 1683 1108.0
4921 1683 1108.0
5454 4391 4161.0
5454 5070 2755.0
5070 4391 2935.0

输出

from collections import defaultdict
paircount = defaultdict(int)
pairtime = defaultdict(float)
pairper = defaultdict(float)
timeavg = defaultdict(float)


#get number of pair occurrences and total time
with open('input.txt', 'r') as f, open('output.txt', 'w') as o:
    for numline, line in enumerate((line.split() for line in f), start=1):
        pair = line[0], line[1]
        paircount[pair] += 1
        pairtime[pair] += float(line[2])
    timeavg = pairtime[pair]/numline
    #pairper = dict((pair, c * 100.0 / numline) for (pair, c) in paircount.iteritems())
    for pair, freq in paircount.iteritems():
        #print pair[0], pair[1], c, pairper[pair], pairtime[pair]
        o.write("%s %s %s %s %s \n" % (pair[0], pair[1], freq, pairtime[pair], timeavg))
print 'done'

1 个答案:

答案 0 :(得分:0)

您想要计算每对的平均值,因此您必须将timeavg计算放入第二个循环中:

from collections import defaultdict
paircount = defaultdict(int)
pairtime = defaultdict(float)
pairper = defaultdict(float)
timeavg = defaultdict(float)


#get number of pair occurrences and total time
with open('input.txt', 'r') as f, open('output.txt', 'w') as o:
    for numline, line in enumerate((line.split() for line in f), start=1):
        pair = line[0], line[1]
        paircount[pair] += 1
        pairtime[pair] += float(line[2])

    for pair, freq in paircount.iteritems():
        timeavg = pairtime[pair] / freq
        o.write("%s %s %s %s %s \n" % (pair[0], pair[1], freq, pairtime[pair], timeavg))

print 'done'

现在输出是:

4945 3545 3 993.0 331.0 
4602 183 1 2280.0 2280.0 
4391 2814 1 16.0 16.0 
5045 3545 1 896.0 896.0 
4921 1683 1 1108.0 1108.0 
5454 5070 1 2755.0 2755.0 
3545 3140 2 67.0 33.5 
4921 3545 1 896.0 896.0 
5045 4921 1 63078.0 63078.0 
5070 4391 1 2935.0 2935.0 
5454 4391 1 4161.0 4161.0 
5045 1683 1 1108.0 1108.0