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

时间:2022-06-01 21:23:05

I want to convert a = [1,2,3,4,5] into a_string = "1 2 3 4 5". The real numpy array is quite big (50000x200) so I assume using for loops is too slow.

我想将a = [1,2,3,4,5]转换为a_string =“1 2 3 4 5”。真正的numpy数组相当大(50000x200)所以我认为使用for循环太慢了。

5 个解决方案

#1


34  

You can use the join method from string:

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

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

#2


8  

np.savetxt

import numpy as np
import sys

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

Output:

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

Or if the array is inverted:

或者如果数组被反转:

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

Or if you really need a string:

或者如果你真的需要一个字符串:

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

You can control precision with fmt to obtain:

您可以使用fmt控制精度以获得:

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

output:

0.000 1.000 2.000 3.000 

or:

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

output:

0 1 2 3

Tested on Python 2.7.12, numpy 1.11.1.

在Python 2.7.12上测试,numpy 1.11.1。

#3


3  

Numpy provides two functions for this array_str and array_repr -- either of which should fit your needs. Since you could use either, here's an example of each:

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]])'

These two functions are both highly optimized and, as such, should be preferred over a function you might write yourself. When dealing with arrays this size, I'd imagine you'd want all the speed you can get.

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

#4


3  

If you have a numpy array to begin with rather than a list (since you mention a "real numpy array" in your post) you could use re.sub on the string representation of the array:

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

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

Again, this is assuming your array a was a numpy array at some point. This has the advantage of working on matrices as well.

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

#5


1  

Maybe a bit hacky, but I just slice them off after using np.array2string so:

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

Result:

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

np.array2string has lots of options also, so you can set your column width which can be very helpful with lots of data:

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

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

Gives:

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

And it will smartly split at the array elements. Note the space appended to the beginning of the string to account for aligning the first row after removing the initial bracket.

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

#1


34  

You can use the join method from string:

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

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

#2


8  

np.savetxt

import numpy as np
import sys

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

Output:

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

Or if the array is inverted:

或者如果数组被反转:

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

Or if you really need a string:

或者如果你真的需要一个字符串:

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

You can control precision with fmt to obtain:

您可以使用fmt控制精度以获得:

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

output:

0.000 1.000 2.000 3.000 

or:

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

output:

0 1 2 3

Tested on Python 2.7.12, numpy 1.11.1.

在Python 2.7.12上测试,numpy 1.11.1。

#3


3  

Numpy provides two functions for this array_str and array_repr -- either of which should fit your needs. Since you could use either, here's an example of each:

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]])'

These two functions are both highly optimized and, as such, should be preferred over a function you might write yourself. When dealing with arrays this size, I'd imagine you'd want all the speed you can get.

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

#4


3  

If you have a numpy array to begin with rather than a list (since you mention a "real numpy array" in your post) you could use re.sub on the string representation of the array:

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

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

Again, this is assuming your array a was a numpy array at some point. This has the advantage of working on matrices as well.

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

#5


1  

Maybe a bit hacky, but I just slice them off after using np.array2string so:

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

Result:

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

np.array2string has lots of options also, so you can set your column width which can be very helpful with lots of data:

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

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

Gives:

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

And it will smartly split at the array elements. Note the space appended to the beginning of the string to account for aligning the first row after removing the initial bracket.

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