如何打印没有括号的Numpy数组?

时间:2012-02-20 11:05:28

标签: python numpy

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

7 个答案:

答案 0 :(得分:40)

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

>>> a = [1,2,3,4,5]
>>> ' '.join(map(str, a))
"1 2 3 4 5"

答案 1 :(得分:11)

<强> np.savetxt

Python 3(see also):

import numpy as np
import sys

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

Python 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

获取字符串而不是打印

Python 3:

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

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

Python 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

答案 2 :(得分:3)

Numpy为此array_strarray_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]])'

这两个功能都经过高度优化,因此应优先于您自己编写的功能。处理这么大的数组时,我想你会想要所有速度。

答案 3 :(得分:3)

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

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

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

答案 4 :(得分:2)

也许有点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

它将巧妙地拆分数组元素。请注意,在字符串的开头附加了空格,以便在删除初始方​​括号后对齐第一行。

答案 5 :(得分:1)

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

与列表相同

答案 6 :(得分:0)

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

template(name="myfwd" type="string"
     string="<%PRI%>%TIMESTAMP% %HOSTNAME% dummytag %rawmsg%\n")
if $fromhost == 'thehost' then {
    action(type="omfwd" target="theserver" protocol="tcp" template="myfwd")
}
   

输出:

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)
相关问题