当我打印一个numpy数组时,我得到了一个截断的表示,但我想要完整的数组。

>>> numpy.arange(10000)
array([   0,    1,    2, ..., 9997, 9998, 9999])

>>> numpy.arange(10000).reshape(250,40)
array([[   0,    1,    2, ...,   37,   38,   39],
       [  40,   41,   42, ...,   77,   78,   79],
       [  80,   81,   82, ...,  117,  118,  119],
       ..., 
       [9880, 9881, 9882, ..., 9917, 9918, 9919],
       [9920, 9921, 9922, ..., 9957, 9958, 9959],
       [9960, 9961, 9962, ..., 9997, 9998, 9999]])

当前回答

这是最黑客的解决方案,它甚至可以像numpy一样打印得很好:

import numpy as np

a = np.arange(10000).reshape(250,40)

b = [str(row) for row in a.tolist()]

print('\n'.join(b))

Out:

其他回答

这是对neoks答案的轻微修改(删除了向set_printoptions传递额外参数的选项)。

它展示了如何使用contextlib.contextmanager轻松创建这样的contextmanager,只需更少的代码行:

import numpy as np
from contextlib import contextmanager

@contextmanager
def show_complete_array():
    oldoptions = np.get_printoptions()
    np.set_printoptions(threshold=np.inf)
    try:
        yield
    finally:
        np.set_printoptions(**oldoptions)

在您的代码中,可以这样使用:

a = np.arange(1001)

print(a)      # shows the truncated array

with show_complete_array():
    print(a)  # shows the complete array

print(a)      # shows the truncated array (again)

关闭并返回正常模式

np.set_printoptions(threshold=False)

这是最黑客的解决方案,它甚至可以像numpy一样打印得很好:

import numpy as np

a = np.arange(10000).reshape(250,40)

b = [str(row) for row in a.tolist()]

print('\n'.join(b))

Out:

使用numpy.set_printoptions:

import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)

听起来你在用numpy。

如果是这种情况,您可以添加:

import numpy as np
np.set_printoptions(threshold=np.nan)

这将禁用拐角打印。有关详细信息,请参阅本NumPy教程。