pycharm换行符,使用大型矩阵时,禁止在Pycharm输出中自动换行

I'm working in PyCharm on Windows. In the project I'm currently working on I have "large" matrices, but when i output them Pycharm automatically adds linebreaks so that one row occupys two lines instead of just one:

[[ 3. -1.73205081 0. 0. 0. 0. 0.

0. 0. 0. ]

[-1.73205081 1. -1. -2. 0. 0. 0.

0. 0. 0. ]

[ 0. -1. 1. 0. -1.41421356 0. 0.

0. 0. 0. ]

[ 0. -2. 0. 1. -1.41421356 0.

-1.73205081 0. 0. 0. ]

[ 0. 0. -1.41421356 -1.41421356 0. -1.41421356

0. -1.41421356 0. 0. ]

[ 0. 0. 0. 0. -1.41421356 0. 0.

0. -1. 0. ]

[ 0. 0. 0. -1.73205081 0. 0. 3.

-1.73205081 0. 0. ]

[ 0. 0. 0. 0. -1.41421356 0.

-1.73205081 1. -2. 0. ]

[ 0. 0. 0. 0. 0. -1. 0.

-2. 0. -1.73205081]

[ 0. 0. 0. 0. 0. 0. 0.

0. -1.73205081 0. ]]

It make my results very hard to reed and to compare. The window is big enough so that everything should be displayed but it still breaks the rows. Is there any setting to prevent this?

Thanks in advance!

解决方案

PyCharm default console width is set to 80 characters.

Lines are printed without wrapping unless you set soft wrap in options:

File -> Settings -> Editor -> General -> Console -> Use soft wraps in console.

However both options make reading big matrices hard.

You can fix this in few ways.

With this test code:

import random

m = [[random.random() for a in range(10)] for b in range(10)]

print(m)

You can try one of these:

Pretty print

Use pprint module, and override line width:

import pprint

pprint.pprint(m, width=300)

Numpy

For numpy version 1.13 and lower:

If you use numpy module, configure arrayprint option:

import numpy

numpy.core.arrayprint._line_width = 300

print(numpy.matrix(m))

For numpy version 1.14 and above (thanks to @Alex Johnson):

import numpy

numpy.set_printoptions(linewidth=300)

print(numpy.matrix(m))

Pandas

If you use pandas module, configure display.width option:

import pandas

pandas.set_option('display.width', 300)

print(pandas.DataFrame(m))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是根据题目描述实现的完整代码,注释有详细解释: ```python # 定义一个带权有向图类 class WeightedDigraph: def __init__(self, vertices): self.vertices = vertices self.num_vertices = len(vertices) self.adj_matrix = [[0] * self.num_vertices for _ in range(self.num_vertices)] def add_edge(self, start_vertex, end_vertex, weight): start_idx = self.vertices.index(start_vertex) end_idx = self.vertices.index(end_vertex) self.adj_matrix[start_idx][end_idx] = weight def floyd(self): # 初始化路径矩阵和距离矩阵 dist = self.adj_matrix.copy() path = [[None] * self.num_vertices for _ in range(self.num_vertices)] for i in range(self.num_vertices): for j in range(self.num_vertices): if dist[i][j] != float('inf') and i != j: path[i][j] = i # Floyd算法核心代码 for k in range(self.num_vertices): for i in range(self.num_vertices): for j in range(self.num_vertices): if dist[i][j] > dist[i][k] + dist[k][j]: dist[i][j] = dist[i][k] + dist[k][j] path[i][j] = path[k][j] return dist, path # 从用户输入获取各单位名称、距离和频度数据 num_units = int(input("请输入单位个数:")) units = [] distances = {} frequencies = {} for i in range(num_units): unit = input("请输入第{}个单位名称:".format(i+1)) distance = float(input("请输入{}到超市的距离:".format(unit))) frequency = float(input("请输入{}单位人员到超市的频度:".format(unit))) units.append(unit) distances[unit] = distance frequencies[unit] = frequency # 构建带权有向图 graph = WeightedDigraph(units) # 添加边及边权值 for start_vertex in units: for end_vertex in units: if start_vertex != end_vertex: weight = distances[end_vertex] * frequencies[end_vertex] graph.add_edge(start_vertex, end_vertex, weight) # 调用Floyd算法求解最短路径 dist, path = graph.floyd() # 找出总权值最小的单位 min_row_sum = float('inf') min_row_idx = -1 for i in range(graph.num_vertices): row_sum = sum(dist[i]) if row_sum < min_row_sum: min_row_sum = row_sum min_row_idx = i optimal_unit = units[min_row_idx] print("超市最优选址为:", optimal_unit) ``` 与前面的代码相比,这份代码主要在输入数据的部分有所改动。在这里,我们通过`input()`函数从用户输入获取各单位名称、距离和频度数据,并构建带权有向图来进行计算。其余部分与前面的代码基本相同。 需要注意的是,由于用户输入的数据可能存在错误,如空格、换行符等,因此在实际使用需要对输入数据进行合理的格式化处理,以避免引入不必要的错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值