Pycharm run 输出界面控制一行能够输出的元素个数
今天遇到了一个问题,当我们在 Pycharm
中打印输出数组时,如果数组一行的元素个数过多,那么我们在打印时就会出现以下问题。
代码如下:
import numpy as np
x = np.array([[0., 0.74095432, 0.34067802, 0.93589374, 0.60599073, 0.93659449, 0.91124856, 0.59321356, 0.27561751, 0.70911222],
[0.74095432, 0., 0.54115315, 0.4357432, 0.80746999, 0.21102354, 0.67268649, 0.22495084, 0.46534491, 0.28618016],
[0.34067802, 0.54115315, 0., 0.61422934, 0.35654215, 0.75217493, 0.57200052, 0.49900068, 0.23310825, 0.64472359],
[0.93589374, 0.4357432, 0.61422934, 0., 0.67281411, 0.52407472, 0.31520226, 0.63212897, 0.70277376, 0.72117692],
[0.60599073, 0.80746999, 0.35654215, 0.67281411, 0., 1.00738802, 0.45489512, 0.82998585, 0.58893372, 0.97604723],
[0.93659449, 0.21102354, 0.75217493, 0.52407472, 1.00738802, 0., 0.81177589, 0.36384873, 0.66251773, 0.33324201],
[0.91124856, 0.67268649, 0.57200052, 0.31520226, 0.45489512, 0.81177589, 0., 0.81487324, 0.75085697, 0.93596837],
[0.59321356, 0.22495084, 0.49900068, 0.63212897, 0.82998585, 0.36384873, 0.81487324, 0., 0.33327614, 0.14653099],
[0.27561751, 0.46534491, 0.23310825, 0.70277376, 0.58893372, 0.66251773, 0.75085697, 0.33327614, 0., 0.46593822],
[0.70911222, 0.28618016, 0.64472359, 0.72117692, 0.97604723, 0.33324201, 0.93596837, 0.14653099, 0.46593822, 0.]])
print(x)
输出结果为:
我们可以看到,此时输出在一行中不能完全显示数组中一整行数据,导致我们查看二维数组的结果变得非常困难,解决这个问题有两种方式,一种是使用 Debug
功能中的 View as array
。这里我们介绍另一种方法,即控制 run
界面的打印输出长度。
代码如下:
import numpy as np
desired_width = 200
np.set_printoptions(linewidth=desired_width)
x = np.array([[0., 0.74095432, 0.34067802, 0.93589374, 0.60599073, 0.93659449, 0.91124856, 0.59321356, 0.27561751, 0.70911222],
[0.74095432, 0., 0.54115315, 0.4357432, 0.80746999, 0.21102354, 0.67268649, 0.22495084, 0.46534491, 0.28618016],
[0.34067802, 0.54115315, 0., 0.61422934, 0.35654215, 0.75217493, 0.57200052, 0.49900068, 0.23310825, 0.64472359],
[0.93589374, 0.4357432, 0.61422934, 0., 0.67281411, 0.52407472, 0.31520226, 0.63212897, 0.70277376, 0.72117692],
[0.60599073, 0.80746999, 0.35654215, 0.67281411, 0., 1.00738802, 0.45489512, 0.82998585, 0.58893372, 0.97604723],
[0.93659449, 0.21102354, 0.75217493, 0.52407472, 1.00738802, 0., 0.81177589, 0.36384873, 0.66251773, 0.33324201],
[0.91124856, 0.67268649, 0.57200052, 0.31520226, 0.45489512, 0.81177589, 0., 0.81487324, 0.75085697, 0.93596837],
[0.59321356, 0.22495084, 0.49900068, 0.63212897, 0.82998585, 0.36384873, 0.81487324, 0., 0.33327614, 0.14653099],
[0.27561751, 0.46534491, 0.23310825, 0.70277376, 0.58893372, 0.66251773, 0.75085697, 0.33327614, 0., 0.46593822],
[0.70911222, 0.28618016, 0.64472359, 0.72117692, 0.97604723, 0.33324201, 0.93596837, 0.14653099, 0.46593822, 0.]])
print(x)
得到的打印结果如下:
现在我们就很容易看出二维数组的特征了。
如果大家觉得有用,就点个赞让更多的人看到吧~