我有以下转换数字列表的函数:

import numpy as np
from math import *


def walsh_transform(x):
    if len(x) > 3:
        n = len(x)
        m = trunc(log(n, 2))
        x = x[0:2 ** m]
        h2 = [[1, 1], [1, -1]]
        for i in range(m - 1):
            if i == 0:
                h = np.kron(h2, h2)
            else:
                h = np.kron(h, h2)

        return np.dot(h, x) / 2. ** m

arr = [1.0, 1.0, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0]

print(walsh_transform(arr))

它返回输出 [ 0.625 -0.125 -0.125 0.125 0.625 -0.125 -0.125 0.125]
我怎样才能让它返回输出 [0.625, -0.125, -0.125, 0.125, 0.625, -0.125, -0.125, 0.125] ? IE。逗号分隔值?

最佳答案

只需将最终结果转换为列表,因为列表会以您想要的格式打印出来。

print(list(walsh_transform(arr)))

关于python - 在python中格式化输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47267849/

10-11 17:27