【问题标题】:string representation of a numpy array with commas separating its elementsnumpy 数组的字符串表示形式,用逗号分隔其元素
【发布时间】:2025-12-29 23:55:06
【问题描述】:

我有一个numpy数组,例如:

points = np.array([[-468.927,  -11.299,   76.271, -536.723],
                   [-429.379, -694.915, -214.689,  745.763],
                   [   0.,       0.,       0.,       0.   ]])

如果我打印它或使用 str() 将其转换为字符串,我会得到:

print w_points
[[-468.927  -11.299   76.271 -536.723]
 [-429.379 -694.915 -214.689  745.763]
 [   0.       0.       0.       0.   ]]

我需要把它变成一个用逗号分隔的字符串,同时保持二维数组结构,即:

[[-468.927,  -11.299,   76.271, -536.723],
 [-429.379, -694.915, -214.689,  745.763],
 [   0.,       0.,       0.,       0.   ]]

有人知道将 numpy 数组转换为那种形式的字符串的简单方法吗?

我知道 .tolist() 添加了逗号,但结果丢失了 2D 结构。

【问题讨论】:

  • numpy.set_printoptions 真的应该有这个选项

标签: python numpy


【解决方案1】:

尝试使用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.   ]])

如果您打算使用大型 numpy 数组,请先设置 np.set_printoptions(threshold=np.nan)。没有它,数组表示将在大约 1000 个条目后被截断(默认情况下)。

>>> arr = np.arange(1001)
>>> print(repr(arr))
array([   0,    1,    2, ...,  998,  999, 1000])

当然,如果你有这么大的数组,这开始变得不那么有用了,你可能应该以某种方式分析数据,而不仅仅是查看它,并且有 better ways 持久化一个 numpy 数组而不是保存它 @987654326 @ 到一个文件...

【讨论】:

    【解决方案2】:

    现在,在 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]]
    

    与来自@mgilson 的repr 比较(显示“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)
    

    附:大型数组仍需要np.set_printoptions(threshold=np.nan)

    【讨论】:

    • 您也可以将“threshold=np.nan”传递给 numpy.array2string 函数。
    【解决方案3】:

    另一种方法是使用 Python 的 pprint 模块(它具有各种格式选项),当对象没有 __repr__() 方法时特别有用。以下是它的样子,例如:

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

    【讨论】:

      【解决方案4】:

      您要查找的函数是np.set_string_functionsource

      此函数的作用是让您覆盖 numpy 对象的默认 __str____repr__ 函数。如果将repr 标志设置为True,__repr__ 函数将被您的自定义函数覆盖。同样,如果您设置repr=False__str__ 函数将被覆盖。由于print调用了对象的__str__函数,所以我们需要设置repr=False

      例如:

      np.set_string_function(lambda x: repr(x), repr=False)
      x = np.arange(5)
      print(x)
      

      将打印输出

      array([0, 1, 2, 3, 4])
      

      一个更美观的版本是

      np.set_string_function(lambda x: repr(x).replace('(', '').replace(')', '').replace('array', '').replace("       ", ' ') , repr=False)
      
      print(np.eye(3))
      

      给了

      [[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]]
      

      希望这能回答你的问题。

      【讨论】:

        最近更新 更多