在一行上打印 `numpy.ndarray`

Print `numpy.ndarray` on a single line(在一行上打印 `numpy.ndarray`)
本文介绍了在一行上打印 `numpy.ndarray`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在使用 scipy/numpy 时,我确实获得了存储在 numpy.ndarray

While using scipy/numpy, I do get information that I store into a numpy.ndarray

>>> a
array([[ 0.15555605,  0.51031528,  0.84580176,  0.06722675],
       [ 0.60556045,  0.62721023, -0.48979983, -0.04152777],
       [-0.78044785,  0.58837543, -0.21146041, -0.13568023],
       [ 0.        ,  0.        ,  0.        ,  1.        ]])
>>> print(a)
[[ 0.15555605  0.51031528  0.84580176  0.06722675]
 [ 0.60556045  0.62721023 -0.48979983 -0.04152777]
 [-0.78044785  0.58837543 -0.21146041 -0.13568023]
 [ 0.          0.          0.          1.        ]]

如何将结果打印在一行上?

How can I print the result on a single line?

我已经检查过了:

>>> numpy.get_printoptions()
{'precision': 8, 'threshold': 1000, 'edgeitems': 3, 'linewidth': 75, 'suppress': False, 'nanstr': 'nan', 'infstr': 'inf', 'formatter': None}

但即使将 linewidth 设置为 1000 也不会改变这一点.有没有办法改变该类型的显示格式?

But even setting linewidth to 1000 does no change this. Is there a way to change the displayed format of that type?

是否也可以在每个数字之间添加逗号(如数组显示但没有周围的 array(...))?

Is it also possible to add comma in between each number (like the array display but without the surrounding array(...))?

推荐答案

为了将 numpy.array 打印成单行,您可以使用其内置函数将其转换为列表numpy.tolist()

In order to print a numpy.array into a single line, you can convert it to a list with its built-in function numpy.tolist()

例子:

import numpy as np

arr = np.array(((1, 2, 3), (4, 5, 6), (7, 8, 9)))

数组的简单打印:

print(arr)
[[1, 2, 3]
 [4, 5, 6]
 [7, 8, 9]]

numpy.tolist() 相比:

print(array.tolist())
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

这篇关于在一行上打印 `numpy.ndarray`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Why does Python#39;s IDLE crash when I type a parenthesis on Mac?(为什么我在 Mac 上键入括号时 Python 的 IDLE 会崩溃?)
Tkinter crashes Jupyter kernel?(Tkinter 崩溃 Jupyter 内核?)
IDLE crash when opening on Mac OS X(在 Mac OS X 上打开时 IDLE 崩溃)
GVIM crashes when running python(运行 python 时 GVIM 崩溃)
PySide: Segfault(?) when using QItemSelectionModel with QListView(PySide:将 QItemSelectionModel 与 QListView 一起使用时的 Segfault(?))
Why does PyQt sometimes crash on exit?(为什么 PyQt 有时会在退出时崩溃?)