我遇到了问题,因为我输出的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]
但是,我需要用逗号分隔每一行,以便它能在这段代码中工作:
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矢量的情况下完成此操作?
答案 0 :(得分:2)
从你的other question推断,我认为你可以用这样的东西做你想做的事:
def mean(a):
return sum(a) / len(a)
averages = []
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]
averages.append(A)
print map(mean, zip(*averages))
或者,可以使用与此类似的代码更简洁地完成:
def mean(a):
return sum(a) / len(a)
averages = []
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 = [mean(map(float, (a, b, c))),
mean(map(float, (d, e, f))),
mean(map(float, (g, h, i))),
mean(map(float, (j, k, l))),
mean(map(float, (m, n, o))),
mean(map(float, (p, q, r))),
mean(map(float, (s, t, u)))]
averages.append(A)
print map(mean, zip(*averages))
更简洁地说:
def mean(a):
return sum(a) / len(a)
averages = []
with open("test2.xls") as w:
w.next() # skip over header row
for row in w:
cols = row.split("\t") # split into columns
# then split that into fields
date, time, values, LZA, SZA, LAM = (cols[0], cols[1], cols[2:23],
cols[23], cols[24], cols[25])
A = [mean(map(float, values[i:i+3])) for i in xrange(0, 21, 3)]
averages.append(A)
print map(mean, zip(*averages))
在最后一个中,如果您愿意,可以将averages
重命名为a
,因为不再有名为a
的字段会与之发生冲突。无论如何,所有代码片段都会打印相同的答案。
答案 1 :(得分:1)
您可以使用list comprehension
将浮动列表转换为字符串列表','.join(str(f) for f in A)
答案 2 :(得分:1)
您正在尝试生成列表列表。但是,您没有保存每个列表 - 每次执行循环时,您都会创建一个新记录,然后将其替换为下一个记录。这就是为什么最终只包含最后一条记录。
逗号不相关 - 这只是Python的语法。这些值不会在内部用逗号存储!
不是将每条记录分配给A,而是将A初始化为空列表,然后将每条新记录添加到最后。
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.append([(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])
答案 3 :(得分:0)
使用str()
将它们转换为字符串,然后再将其转换为str.join
,这根本不会影响原始项目:
>>> lis = [0.018801, 0.011839, -3332.980568, 0.009446, -3332.984916, 0.007438, -3332.982958]
>>> print ", ".join(str(x) for x in lis)
0.018801, 0.011839, -3332.980568, 0.009446, -3332.984916, 0.007438, -3332.982958
列表仍包含浮点数:
>>> lis
[0.018801, 0.011839, -3332.980568, 0.009446, -3332.984916, 0.007438, -3332.982958]