我有以下部分输入文件(超过 500 行):
L1, a, b, 10, 20, pass,
L1, c, d, 11, 21, pass,
L1, e, f, 12, 22, pass,
L1, a, b, 13, 23, pass,
L1, e, f, 14, 34, pass,
我想获得重复项的平均值,即输出如下:
(对于 L1,a,b,11.5 = (10+13)/2, 21.5 = (20+23)/2)
L1, a, b, 11.5, 21.5
L1, c, d, 11, 21
L1, e, f, 13, 28
我目前的初学者 python 代码如下 - 仍在努力更好地调整它
import csv
from collections import defaultdict
import numpy as np
dd = defaultdict(list)
with open("mean.csv") as input_file:
for row in csv.reader(input_file):
dd[tuple(row[:3])].append(float(row[3]))
dd[tuple(row[:3])].append(float(row[4]))
for k, v, m in dd.iteritems():
if len(v) > 1:
print (' '.join(k), np.mean(v), np.mean(m))
我得到的错误是:
Traceback (most recent call last):
File "average.py", line 11, in <module>
for k, v, m in dd.iteritems():
ValueError: need more than 2 values to unpack