打印结果没有标记'数组'在列表中?

时间:2018-06-17 01:38:26

标签: python numpy

我想在没有标签ARRAY的情况下显示我的结果,然后列表中的数组

我的结果是:

[array([202.632   , 565.74    , 177.258   ,   0.01627 ,   0.00008 ,
     0.00919 ,   0.00963 ,   0.02756 ,   0.0717  ,   0.833   ,
     0.03515 ,   0.04265 ,   0.0646  ,   0.10546 ,   0.07889 ,
    14.989   ,   0.427627,   0.775708,  -4.892495,   0.262281,
     2.910213,   0.270173,   1.      ]), array([116.879   , 131.897   , 108.153   ,   0.00788 ,   0.00007 ,
     0.00334 ,   0.00493 ,   0.01003 ,   0.02645 ,   0.265   ,
     0.01394 ,   0.01625 ,   0.02137 ,   0.04183 ,   0.00786 ,
    22.603   ,   0.540049,   0.813432,  -4.476755,   0.262633,
     1.827012,   0.326197,   1.      ]), array([169.774   , 191.759   , 151.451   ,   0.01568 ,   0.00009 ,
     0.00863 ,   0.00946 ,   0.02589 ,   0.08143 ,   0.821   ,
     0.03804 ,   0.05426 ,   0.08808 ,   0.11411 ,   0.0753  ,
    12.359   ,   0.56161 ,   0.793509,  -3.297668,   0.414758,
     3.413649,   0.457533,   1.      ])]

我希望我的结果如下:

[[202.632   , 565.74    , 177.258   ,   0.01627 ,   0.00008 ,
     0.00919 ,   0.00963 ,   0.02756 ,   0.0717  ,   0.833   ,
     0.03515 ,   0.04265 ,   0.0646  ,   0.10546 ,   0.07889 ,
    14.989   ,   0.427627,   0.775708,  -4.892495,   0.262281,
     2.910213,   0.270173,   1.      ], [116.879   , 131.897   , 108.153   ,   0.00788 ,   0.00007 ,
     0.00334 ,   0.00493 ,   0.01003 ,   0.02645 ,   0.265   ,
     0.01394 ,   0.01625 ,   0.02137 ,   0.04183 ,   0.00786 ,
    22.603   ,   0.540049,   0.813432,  -4.476755,   0.262633,
     1.827012,   0.326197,   1.      ], [169.774   , 191.759   , 151.451   ,   0.01568 ,   0.00009 ,
     0.00863 ,   0.00946 ,   0.02589 ,   0.08143 ,   0.821   ,
     0.03804 ,   0.05426 ,   0.08808 ,   0.11411 ,   0.0753  ,
    12.359   ,   0.56161 ,   0.793509,  -3.297668,   0.414758,
     3.413649,   0.457533,   1.      ]]

2 个答案:

答案 0 :(得分:0)

您可以使用reprlibsys.displayhook

以下代码非常紧密地遵循链接的Python文档中的代码片段:

import sys
import reprlib
import builtins

class MyRepr(reprlib.Repr):

    def repr_ndarray(self, obj, level):
        return repr(obj).replace('array(', '')[:-1]

myrepr = MyRepr()

def mydisplayhook(value):
    if value is None:
        return
    # Set '_' to None to avoid recursion
    builtins._ = None
    text = myrepr.repr(value)
    try:
        sys.stdout.write(text)
    except UnicodeEncodeError:
        bytes = text.encode(sys.stdout.encoding, 'backslashreplace')
        if hasattr(sys.stdout, 'buffer'):
            sys.stdout.buffer.write(bytes)
        else:
            text = bytes.decode(sys.stdout.encoding, 'strict')
            sys.stdout.write(text)
    sys.stdout.write("\n")
    builtins._ = value

sys.displayhook = mydisplayhook

现在例如:

[np.arange(5)]
# [[0, 1, 2, 3, 4]]

注意:这也可能会影响其他类型的显示。 (我认为默认情况下reprlib.Repr缩短了一些内容。)为了防止这种情况,请查看docs

答案 1 :(得分:-1)

看起来像一个包含数组元素的列表(或者可能不止一个):

In [11]: alist = [np.array([1,2,3])]
In [12]: alist
Out[12]: [array([1, 2, 3])]
In [13]: print(alist)
[array([1, 2, 3])]

我们可以遍历列表并将每个数组转换为等效的列表:

In [14]: [x.tolist() for x in alist]
Out[14]: [[1, 2, 3]]

这是一个嵌套列表。或者我们可以把它变成一个数组:

In [15]: arr = np.stack(alist)
In [16]: arr
Out[16]: array([[1, 2, 3]])
In [17]: print(arr)
[[1 2 3]]

数组的str显示不包含array字。这个stack只有在成分列表形状相同的情况下才有效。

我们必须将数组列表转换为其他方式 - 数组或嵌套列表。我们无法改变列表的显示方式。

当我复制粘贴您的列表(使用array=np.array)时,我会显示一个列表:

In [35]: yrlist
Out[35]: 
[array([ 2.026320e+02,  5.657400e+02,  1.772580e+02,  1.627000e-02,
         8.000000e-05,  9.190000e-03,  9.630000e-03,  2.756000e-02,
         7.170000e-02,  8.330000e-01,  3.515000e-02,  4.265000e-02,
         6.460000e-02,  1.054600e-01,  7.889000e-02,  1.498900e+01,
         4.276270e-01,  7.757080e-01, -4.892495e+00,  2.622810e-01,
         2.910213e+00,  2.701730e-01,  1.000000e+00]),
 array([ 1.168790e+02,  1.318970e+02,  1.081530e+02,  7.880000e-03,
         7.000000e-05,  3.340000e-03,  4.930000e-03,  1.003000e-02,
          ....
         3.413649e+00,  4.575330e-01,  1.000000e+00])]

请注意,这些数组不仅会显示数组'标签,但显示所有具有相同格式的元素,科学记数法对于此范围的值最佳。

如果我为每个数组指定dtype=object

In [37]: olist
Out[37]: 
[array([202.632, 565.74, 177.258, 0.01627, 8e-05, 0.00919, 0.00963,
        0.02756, 0.0717, 0.833, 0.03515, 0.04265, 0.0646, 0.10546, 0.07889,
        14.989, 0.427627, 0.775708, -4.892495, 0.262281, 2.910213,
        0.270173, 1.0], dtype=object),
         ....
        0.0753, 12.359, 0.56161, 0.793509, -3.297668, 0.414758, 3.413649,
        0.457533, 1.0], dtype=object)]

每个数组元素在输入时显示很多。但也有dtype符号。

对于此较长数组的列表,tolist方法显示为:

In [42]: print(str([a.tolist() for a in yrlist]))
[[202.632, 565.74, 177.258, 0.01627, 8e-05, 0.00919, 0.00963, 0.02756, 0.0717, 0.833, 0.03515, 0.04265, 0.0646, 0.10546, 0.07889, 14.989, 0.427627, 0.775708, -4.892495, 0.262281, 2.910213, 0.270173, 1.0], [116.879, 131.897, 108.153, 0.00788, 7e-05, 0.00334, 0.00493, 0.01003, 0.02645, 0.265, 0.01394, 0.01625, 0.02137, 0.04183, 0.00786, 22.603, 0.540049, 0.813432, -4.476755, 0.262633, 1.827012, 0.326197, 1.0], [169.774, 191.759, 151.451, 0.01568, 9e-05, 0.00863, 0.00946, 0.02589, 0.08143, 0.821, 0.03804, 0.05426, 0.08808, 0.11411, 0.0753, 12.359, 0.56161, 0.793509, -3.297668, 0.414758, 3.413649, 0.457533, 1.0]]

没有np.array显示产生的任何换行。

对列表中的每个数组执行独立打印:

In [46]: for a in yrlist:
    ...:     print(a)
    ...:     
[ 2.026320e+02  5.657400e+02  1.772580e+02  1.627000e-02  8.000000e-05
  9.190000e-03  9.630000e-03  2.756000e-02  7.170000e-02  8.330000e-01
  3.515000e-02  4.265000e-02  6.460000e-02  1.054600e-01  7.889000e-02
  1.498900e+01  4.276270e-01  7.757080e-01 -4.892495e+00  2.622810e-01
  2.910213e+00  2.701730e-01  1.000000e+00]
[ 1.168790e+02  1.318970e+02  1.081530e+02  7.880000e-03  7.000000e-05
  3.340000e-03  4.930000e-03  1.003000e-02  2.645000e-02  2.650000e-01
  1.394000e-02  1.625000e-02  2.137000e-02  4.183000e-02  7.860000e-03
  2.260300e+01  5.400490e-01  8.134320e-01 -4.476755e+00  2.626330e-01
  1.827012e+00  3.261970e-01  1.000000e+00]
[ 1.697740e+02  1.917590e+02  1.514510e+02  1.568000e-02  9.000000e-05
  8.630000e-03  9.460000e-03  2.589000e-02  8.143000e-02  8.210000e-01
  3.804000e-02  5.426000e-02  8.808000e-02  1.141100e-01  7.530000e-02
  1.235900e+01  5.616100e-01  7.935090e-01 -3.297668e+00  4.147580e-01
  3.413649e+00  4.575330e-01  1.000000e+00]

将整个事物变成数组

In [48]: print(np.array(yrlist))
[[ 2.026320e+02  5.657400e+02  1.772580e+02  1.627000e-02  8.000000e-05
   9.190000e-03  9.630000e-03  2.756000e-02  7.170000e-02  8.330000e-01
   3.515000e-02  4.265000e-02  6.460000e-02  1.054600e-01  7.889000e-02
   1.498900e+01  4.276270e-01  7.757080e-01 -4.892495e+00  2.622810e-01
   2.910213e+00  2.701730e-01  1.000000e+00]
 [ 1.168790e+02  1.318970e+02  1.081530e+02  7.880000e-03  7.000000e-05
   3.340000e-03  4.930000e-03  1.003000e-02  2.645000e-02  2.650000e-01
   1.394000e-02  1.625000e-02  2.137000e-02  4.183000e-02  7.860000e-03
   2.260300e+01  5.400490e-01  8.134320e-01 -4.476755e+00  2.626330e-01
   1.827012e+00  3.261970e-01  1.000000e+00]
 [ 1.697740e+02  1.917590e+02  1.514510e+02  1.568000e-02  9.000000e-05
   8.630000e-03  9.460000e-03  2.589000e-02  8.143000e-02  8.210000e-01
   3.804000e-02  5.426000e-02  8.808000e-02  1.141100e-01  7.530000e-02
   1.235900e+01  5.616100e-01  7.935090e-01 -3.297668e+00  4.147580e-01
   3.413649e+00  4.575330e-01  1.000000e+00]]