0

我遇到了问题,因为我的输出格式如下:

[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 向量?

4

4 回答 4

2

从你的另一个问题推断,我认为你可以用这样的东西做你想做的事:

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))

在最后一个中,您可以根据需要重命名averagesa,因为不再有一个名称a会与之冲突的字段。无论如何,所有代码片段都将打印相同的答案。

于 2013-07-08T15:56:30.067 回答
1

您可以使用列表推导将浮点数列表转换为字符串列表

','.join(str(f) for f in A)
于 2013-07-08T12:56:15.253 回答
1

您正在尝试生成列表列表。但是,您并没有保存每个列表——每次循环时,您都会创建一条新记录,然后用下一条记录替换它。这就是为什么 a 最终只包含最后一条记录。

逗号不相关——这只是 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])
于 2013-07-08T15:50:55.273 回答
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]
于 2013-07-08T12:55:19.283 回答