Python numpy 数组的字符串表示,用逗号分隔其元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16423774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
string representation of a numpy array with commas separating its elements
提问by martinako
I have a numpy array, for example:
我有一个 numpy 数组,例如:
points = np.array([[-468.927, -11.299, 76.271, -536.723],
[-429.379, -694.915, -214.689, 745.763],
[ 0., 0., 0., 0. ]])
if I print it or turn it into a string with str() I get:
如果我打印它或使用 str() 将其转换为字符串,我会得到:
print w_points
[[-468.927 -11.299 76.271 -536.723]
[-429.379 -694.915 -214.689 745.763]
[ 0. 0. 0. 0. ]]
I need to turn it into a string that prints with separating commas while keeping the 2D array structure, that is:
我需要把它变成一个用分隔逗号打印的字符串,同时保持二维数组结构,即:
[[-468.927, -11.299, 76.271, -536.723],
[-429.379, -694.915, -214.689, 745.763],
[ 0., 0., 0., 0. ]]
Does anybody know an easy way of turning a numpy array to that form of string?
有人知道将 numpy 数组转换为那种形式的字符串的简单方法吗?
I know that .tolist() adds the commas but the result loses the 2D structure.
我知道 .tolist() 添加逗号但结果丢失了二维结构。
采纳答案by mgilson
Try using repr
尝试使用 repr
>>> import numpy as np
>>> points = np.array([[-468.927, -11.299, 76.271, -536.723],
... [-429.379, -694.915, -214.689, 745.763],
... [ 0., 0., 0., 0. ]])
>>> print repr(points)
array([[-468.927, -11.299, 76.271, -536.723],
[-429.379, -694.915, -214.689, 745.763],
[ 0. , 0. , 0. , 0. ]])
If you plan on using large numpy arrays, set np.set_printoptions(threshold=np.nan)first. Without it, the array representation will be truncated after about 1000 entries (by default).
如果您打算使用大型 numpy 数组,np.set_printoptions(threshold=np.nan)请先设置。没有它,数组表示将在大约 1000 个条目后被截断(默认情况下)。
>>> arr = np.arange(1001)
>>> print repr(arr)
array([ 0, 1, 2, ..., 998, 999, 1000])
Of course, if you have arrays that large, this starts to become less useful and you should probably analyze the data some way other than just looking at it and there are better waysof persisting a numpy array than saving it's reprto a file...
当然,如果你有这么大的数组,这开始变得不那么有用了,你可能应该以某种方式分析数据,而不仅仅是查看它,并且有更好的方法来持久化一个 numpy 数组而不是将它保存repr到一个文件......
回答by NYCeyes
Another way to do it, which is particularly helpful when an object doesn't have a __repr__() method, is to employ Python's pprint module (which has various formatting options). Here is what that looks like, by example:
另一种方法(当对象没有 __repr__() 方法时特别有用)是使用 Python 的 pprint 模块(具有各种格式选项)。下面是它的样子,例如:
>>> import numpy as np
>>> import pprint
>>>
>>> A = np.zeros(10, dtype=np.int64)
>>>
>>> print(A)
[0 0 0 0 0 0 0 0 0 0]
>>>
>>> pprint.pprint(A)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
回答by K.Kit
Now, in numpy 1.11, there is numpy.array2string:
现在,在 numpy 1.11 中,有numpy.array2string:
In [279]: a = np.reshape(np.arange(25, dtype='int8'), (5, 5))
In [280]: print(np.array2string(a, separator=', '))
[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]]
Comparing with reprfrom @mgilson (shows "array()" and dtype):
与repr@mgilson比较(显示“array()”和dtype):
In [281]: print(repr(a))
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]], dtype=int8)
P.S. Still need np.set_printoptions(threshold=np.nan)for large array.
PS仍然需要np.set_printoptions(threshold=np.nan)大阵列。

