我遇到了问题,因为我的输出格式如下:
[0.018801, 0.011839, -3332.980568, 0.009446, -3332.984916, 0.007438, -3332.982958]
[0.020493, 0.015735, -3332.980353, 0.013179, -3332.968465, 0.055135, 0.135461]
[0.020678, 0.018212, -3332.983603, 0.011993, 0.097811, 0.014364, 0.099570]
[0.020758, 0.015798, -3332.982745, 0.013539, 0.086793, 0.007399, -3332.984997]
[-3332.992594, 0.014576, -3332.979745, 0.015103, 0.089420, 0.009226, 0.090133]
但是,我需要用逗号分隔每一行,以便它在这段代码中工作:
def mean(a):
return sum(a) / len(a)
a = [A]
print map(mean, zip(*a))
有什么方法可以实现这一点,同时仍然保持 A 作为浮动列表?因为', '.join
需要字符串值,这不允许我取平均值
下面是我用来生成 A 的代码:
with open("test2.xls") as w:
w.next() # skip over header row
for row in w:
(date, time, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t,
u, LZA, SZA, LAM) = row.split("\t") # split columns into fields
A = [(float(a) + float(b) + float(c))/3,
(float(d) + float(e) + float(f))/3,
(float(g) + float(h) + float(i))/3,
(float(j) + float(k) + float(l))/3,
(float(m) + float(n) + float(o))/3,
(float(p) + float(q) + float(r))/3,
(float(s) + float(t) + float(u))/3]
任何帮助表示赞赏
澄清:
我不一定需要更长的列表 我需要的列表必须相同,但每一行都用逗号分隔。所以当我通过这个意思时:
def mean(a):
return sum(a) / len(a)
a = [A]
print map(mean, zip(*a)
我只得到最后一行:
[-3332.992594, 0.014576, -3332.979745, 0.015103, 0.089420, 0.009226, 0.090133]
但是,如果我编写 A 的输出并用逗号分隔每一行,如下所示:
def mean(a):
return sum(a) / len(a)
a = [[0.018801, 0.011839, -3332.980568, 0.009446, -3332.984916, 0.007438, -3332.982958],
[0.020493, 0.015735, -3332.980353, 0.013179, -3332.968465, 0.055135, 0.135461],
[0.020678, 0.018212, -3332.983603, 0.011993, 0.097811, 0.014364, 0.099570],
[0.020758, 0.015798, -3332.982745, 0.013539, 0.086793, 0.007399, -3332.984997],
[-3332.992594, 0.014576, -3332.979745, 0.015103, 0.089420, 0.009226, 0.090133]]
print map(mean, zip(*a))
我得到了所需的输出
[-666.582372, 0.015232, -3332.981403, 0.012652, -1333.1358714, 0.018713, -1333.128558]
或每列的平均值。我怎样才能做到这一点,而不必用逗号手动修改 A 向量?