【问题标题】:Prettier prints to the console with python, any ideas?Prettier 使用 python 打印到控制台,有什么想法吗?
【发布时间】:2023-01-19 00:25:18
【问题描述】:

我有一个由点和字符组成的 10x10 np 数组。角色以随机模式移动,每次移动后我都会将数组打印到控制台,但它看起来又慢又奇怪,我想问问你们是否知道如何让这个看起来更漂亮?我只允许使用标准的 python 库。

class World:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.world = np.empty((self.x, self.y), dtype=object)
        self.world.fill(None)

在这里我创建了数组,其中充满了符号或什么都没有

def print_world(self):
        for i in range(self.x):
            for j in range(self.y):
                if self.world[i, j] == None:
                    print('.', end=' ')
                else:
                    print(self.world[i, j].symb, end=' ')
            print()

这是实际的印刷品,我的问题是如何使它看起来更漂亮,考虑到它的滞后性而不是它的视觉效果。谢谢

【问题讨论】:

  • 你会考虑turtle 一个标准库吗?除非是这样,否则您可能会完整地构建要打印的字符串并一次打印出来。调用 print 很慢。
  • numpy 不是 Python 标准库的一部分
  • @spencerlou 的确如此,但这是一个家庭作业项目,我们可以为此使用 numpy
  • @JonSG 这个项目除了 numpy、time 和 random 之外什么都不允许

标签: python console-application


【解决方案1】:

试试pprint。见doc

import pprint
def print_world(self):
        for i in range(self.x):
            for j in range(self.y):
                if self.world[i, j] == None:
                    pprint('.', end=' ')
                else:
                    pprint(self.world[i, j].symb, end=' ')

【讨论】:

    猜你喜欢
    • 2018-04-15
    • 2022-10-19
    • 2021-12-06
    • 2013-03-09
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    相关资源
    最近更新 更多