44

我想转换a = [1,2,3,4,5]a_string = "1 2 3 4 5". 真正的 numpy 数组非常大(50000x200),所以我认为使用for loops速度太慢。

4

7 回答 7

60

您可以使用join字符串中的方法:

>>> a = [1,2,3,4,5]
>>> ' '.join(map(str, a))
"1 2 3 4 5"
于 2012-02-20T11:12:38.837 回答
25

np.savetxt

Python 3(另见):

import numpy as np
import sys

a = np.array([0.0, 1.0, 2.0, 3.0])
np.savetxt(sys.stdout.buffer, a)

蟒蛇2:

import numpy as np
import sys

a = np.array([0.0, 1.0, 2.0, 3.0])
np.savetxt(sys.stdout, a)

输出:

0.000000000000000000e+00
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00

控制精度

使用fmt

np.savetxt(sys.stdout, a, fmt="%.3f")

输出:

0.000
1.000
2.000
3.000 

或者:

np.savetxt(sys.stdout, a, fmt="%i")

输出:

0
1
2
3

获取字符串而不是打印

蟒蛇 3:

import io
bio = io.BytesIO()
np.savetxt(bio, a)
mystr = bio.getvalue().decode('latin1')
print(mystr, end='')

我们使用latin1是因为文档告诉我们它是使用的默认编码。

蟒蛇2:

import StringIO
sio = StringIO.StringIO()
np.savetxt(sio, a)
mystr = sio.getvalue()
print mystr

全部在一条线上

或者,如果您真的想要全部在一行中:

a = np.array([0.0, 1.0, 2.0, 3.0])
np.savetxt(sys.stdout, a, newline=' ')
print()

输出:

0.000000000000000000e+00 1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00 

TODO:有一个尾随空格。我看到的唯一解决方案是保存到字符串和条带。

在 Python 2.7.15rc1 和 Python 3.6.6、numpy 1.13.3 上测试

于 2017-02-04T23:17:19.890 回答
13

也许有点hacky,但我只是在使用后将它们切掉np.array2string

import numpy as np

a = np.arange(0,10)
a_str = np.array2string(a, precision=2, separator=', ')
print(a_str[1:-1])

结果:

    0, 1, 2, 3, 4, 5, 6, 7, 8, 9

np.array2string也有很多选项,因此您可以设置列宽,这对大量数据非常有用:

a = np.arange(0,15)
a_str = np.array2string(a, precision=2, separator=', ', max_line_width=15)
print(' ' + a_str[1:-1])

给出:

       0,  1,  2,
       3,  4,  5,
       6,  7,  8,
       9, 10, 11,
      12, 13, 14

它会巧妙地分割数组元素。请注意附加到字符串开头的空格,以说明在删除初始括号后对齐第一行。

于 2018-06-19T19:04:34.020 回答
5

如果您以numpy 数组而不是列表开头(因为您在帖子中提到了“真正的 numpy 数组”),您可以re.sub在数组的字符串表示中使用:

print(re.sub('[\[\]]', '', np.array_str(a)))

同样,这是假设您a的数组在某个时候是一个 numpy 数组。这也具有处理矩阵的优点。

于 2016-01-17T00:56:21.440 回答
4

Numpy 为这个 array_str 和 array_repr 提供了两个函数——其中任何一个都应该满足你的需要。由于您可以使用其中任何一个,因此以下是每个示例:

>>> from numpy import arange, reshape, array_str
>>> M = arange(10).reshape(2,5)
>>> M
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> array_str(M)
'[[0 1 2 3 4]\n [5 6 7 8 9]]'
>>> array_repr(M)
'array([[0, 1, 2, 3, 4],\n       [5, 6, 7, 8, 9]])'

这两个函数都经过高度优化,因此应该比您自己编写的函数更受欢迎。在处理这种大小的数组时,我想你会想要你能得到的所有速度。

于 2012-02-20T11:34:35.967 回答
3
>>> a=np.array([1,2,3,4,5])
>>> print(*a)
1 2 3 4 5

>>> print(str(a)[1:-1])
1 2 3 4 5

与列表相同

于 2020-07-17T17:44:51.367 回答
2

如果您正在处理浮点数和二维数组,您还可以执行以下操作:

import numpy as np
np.random.seed(77)

def my_print(x):
    for row in x:
        print(' '.join(map(lambda x: "{:.3f}\t".format(x), row)))

if __name__ == '__main__':
    x = np.random.random(size=(3, 9))
    my_print(x)

输出:

0.919    0.642   0.754   0.139   0.087   0.788   0.326   0.541   0.240  
0.545    0.401   0.715   0.837   0.588   0.296   0.281   0.706   0.423  
0.057    0.747   0.452   0.176   0.049   0.292   0.067   0.751   0.064
于 2020-07-17T17:05:02.200 回答