python中的格式输出

时间:2017-11-13 15:28:11

标签: python python-3.x function numpy

我有以下功能可以转换数字列表:

sum()

返回输出select lastname, firsname, sum(case when upper(category) = 'W' then abs(principal) end) as Withdrawal, sum(case when upper(category) = 'D' then abs(principal) end) as Deposit, description from table1 join table2 on table2.id = table1.id join table3 on table3.c = table2.c where description = 'string' group by lastname, firstname, description

如何让它返回输出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)) ?即逗号分隔值?

5 个答案:

答案 0 :(得分:2)

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

print(list(walsh_transform(arr)))

答案 1 :(得分:1)

您可以使用return [z for z in np.dot(h, x) / 2. ** m]代替return np.dot(h, x) / 2. ** m

答案 2 :(得分:1)

您可以将结果数组转换为list以获得所需的输出。

w = walsh_transform(arr) # w = [ 0.625 -0.125 -0.125  0.125  0.625 -0.125 -0.125  0.125]

print(list(w)) # output = [0.625, -0.125, -0.125, 0.125, 0.625, -0.125, -0.125, 0.125]

答案 3 :(得分:0)

尝试使用repr

print(repr(walsh_transform(arr))) # output = [0.625, -0.125, -0.125, 0.125, 0.625, -0.125, -0.125, 0.125]

或者,您可以将其投射到列表中:

print(list(walsh_transform(arr))) # output = [0.625, -0.125, -0.125, 0.125, 0.625, -0.125, -0.125, 0.125]

答案 4 :(得分:0)

您所要做的就是用逗号替换空白。

re.sub功能也应该起作用。