Все строки столбцов окрашены - PullRequest
0 голосов
/ 20 июня 2020

Вот мой полный код:

#!/usr/bin/python3

from sys import argv
from subprocess import check_output

def common(a, b):
    return list(set(a) & set(b))

class Canvas():
    colors = {
        "gray": 30,
        "red": 31,
        "green": 32,
        "yellow": 33,
        "blue": 34,
        "magenta": 35,
        "cyan": 36,
        "white": 37
    }
    default = 30
    def __init__(self, rows, cols):
        self.rows = rows
        self.cols = cols
        self.points = [[''] * cols] * rows
    def rect(self, x1, y1, x2, y2, color):
        for y in common(range(self.rows), range(y1, y2)):
            for x in common(range(self.cols), range(x1, x2)):
                self.points[y][x] = color
    def draw(self):
        for y in range(self.rows):
            for x in range(self.cols):
                if self.points[y][x] in self.colors:
                    print ("\033[7;%dm \033[0m" % self.colors[self.points[y][x]], end='')
                else:
                    print ("\033[7;%dm \033[0m" % self.default, end='')
            print ()

def main(argv):
    rows, cols = map(int, check_output("stty size", shell=True, executable="/bin/bash").decode("utf-8").split())
    scene = Canvas(rows - 10, cols - 30)
    scene.rect(0, 0, 5, 5, "green")
    scene.draw()

if __name__ == "__main__":
    main(argv[1:])

Это то, что я хочу: Но вот что я вижу:

Почему это происходит? Я не знаю, в чем проблема, но я знаю, что это из функции rect:

...

def rect(self, x1, y1, x2, y2, color):
    for y in common(range(self.rows), range(y1, y2)):
        for x in common(range(self.cols), range(x1, x2)):
            self.points[y][x] = color

...

Перед запуском l oop self.points выглядит так:

[
    ['', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '']
    ['', '', '', '', '', '', '', '']
]

Но после первой итерации:

[
    ['', '', 'green', '', '', '', '', '']
    ['', '', 'green', '', '', '', '', '']
    ['', '', 'green', '', '', '', '', '']
    ['', '', 'green', '', '', '', '', '']
    ['', '', 'green', '', '', '', '', '']
    ['', '', 'green', '', '', '', '', '']
    ['', '', 'green', '', '', '', '', '']
    ['', '', 'green', '', '', '', '', '']
    ['', '', 'green', '', '', '', '', '']
    ['', '', 'green', '', '', '', '', '']
    ['', '', 'green', '', '', '', '', '']
]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...