python - 在python中格式化输出

标签 python python-3.x function numpy

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

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/

相关文章:

python - 生成器在 Python 中有什么类型签名?

python - 解决列表中也被分组的项目

c++ - 将数组转换为函数返回中的指针

c++ - 何时在 C++ 中使用引用作为参数

python - 如何在接口(interface)上获取事件的 ESSID?

python - 在具有多个拆分器的python中拆分字符串

python - 如何在 Python 中连接两个数据框?

python - 为什么这个程序在猜出三次密码后还没有停止?

javascript - 理解 New 与 JavaScript 作用域函数并传递 $

python - Scraper 不会停止点击下一页按钮