在函数中应用嵌套列表推导

时间:2017-11-24 12:58:43

标签: python-2.7 list for-loop

如何在我的程序中的addCoordinate函数的嵌套for循环中应用嵌套列表解析,而不影响输出。我已经得到它以输出中实现的格式打印迷宫,但我希望使我的整个程序更紧凑并提高其时间复杂度。

((* -> *) -> * -> *) ->
((* -> *) -> * -> *) ->
 (* -> *) -> * -> *

我的计划的输出是:

from itertools import product
class Maze:
    mazeboard, x1, y1, list = [], 0, 0, []

    def __init__(self):
        """
        Constructor - You may modify this, but please do not add any extra parameters
        """

    def addCoordinate(self, x, y, blockType):
        #print self.x1
        if self.x1 < x :
            self.x1 = x
        if self.y1 < y:
            self.y1 = y
        if  len(self.mazeboard) <= self.x1 or  len(self.mazeboard) <= self.y1:
            modified_board = [[1 for a in range(self.x1 + 1)] for b in range(self.y1 + 1)]
            for a in range(len(self.mazeboard)):
                for b in range(len(self.mazeboard[a])):
                    modified_board[a][b] = self.mazeboard[a][b]
            self.mazeboard = modified_board
        self.mazeboard[y][x] = blockType

    def printMaze(self):
        for a in range(self.x1 + 1):
            for b in range(self.y1 + 1):
                if self.mazeboard[a][b] == 0:
                    print " ",
                else:
                    print "*",
            print ""


def mazeTest():
    myMaze = Maze()
    myMaze.addCoordinate(1, 0, 0)
    myMaze.addCoordinate(1, 1, 0)
    myMaze.addCoordinate(7, 1, 0)
    myMaze.addCoordinate(1, 2, 0)
    myMaze.addCoordinate(2, 2, 0)
    myMaze.addCoordinate(3, 2, 0)
    myMaze.addCoordinate(4, 2, 0)
    myMaze.addCoordinate(6, 2, 0)
    myMaze.addCoordinate(7, 2, 0)
    myMaze.addCoordinate(4, 3, 0)
    myMaze.addCoordinate(7, 3, 0)
    myMaze.addCoordinate(4, 4, 0)
    myMaze.addCoordinate(7, 4, 0)
    myMaze.addCoordinate(3, 5, 0)
    myMaze.addCoordinate(4, 5, 0)
    myMaze.addCoordinate(7, 5, 0)
    myMaze.addCoordinate(1, 6, 0)
    myMaze.addCoordinate(2, 6, 0)
    myMaze.addCoordinate(3, 6, 0)
    myMaze.addCoordinate(4, 6, 0)
    myMaze.addCoordinate(6, 6, 0)
    myMaze.addCoordinate(7, 6, 0)
    myMaze.addCoordinate(5, 7, 0)
    myMaze.printMaze()
mazeTest()

1 个答案:

答案 0 :(得分:0)

您可以通过几种不同的方式改进:

  1. 创建坐标列表,而不是多次调用addCoordinate()

  2. 通过将迷宫中的字典转换为*字符,然后将它们全部合并为一个可打印的字符串,来改善您的迷宫打印。

  3. 使用product将旧版mazeboard复制到更新后的版本中。

  4. 例如:

    from itertools import product
    
    class Maze:
        mazeboard, x1, y1 = [], 0, 0
    
        def __init__(self):
            """
            Constructor - You may modify this, but please do not add any extra parameters
            """
    
        def addCoordinate(self, x, y, blockType):
            if self.x1 < x :
                self.x1 = x
            if self.y1 < y:
                self.y1 = y
            if  len(self.mazeboard) <= self.x1 or len(self.mazeboard) <= self.y1:
                modified_board = [[1 for a in range(self.x1 + 1)] for b in range(self.y1 + 1)]
                if len(self.mazeboard):
                    for a, b in product(range(len(self.mazeboard)), range(len(self.mazeboard[0]))):
                        modified_board[a][b] = self.mazeboard[a][b]            
    
                self.mazeboard = modified_board
            self.mazeboard[y][x] = blockType
    
        def printMaze(self):
            print '\n'.join(' '.join(map({1:'*', 0:' '}.get, row)) for row in self.mazeboard)
    
    
    def mazeTest():
        myMaze = Maze()
    
        coords = [
            (1, 0, 0), (1, 1, 0), (7, 1, 0), (1, 2, 0), (2, 2, 0), 
            (3, 2, 0), (4, 2, 0), (6, 2, 0), (7, 2, 0), (4, 3, 0), (7, 3, 0), 
            (4, 4, 0), (7, 4, 0), (3, 5, 0), (4, 5, 0), (7, 5, 0), (1, 6, 0), 
            (2, 6, 0), (3, 6, 0), (4, 6, 0), (6, 6, 0), (7, 6, 0), (5, 7, 0)]
    
        for coord in coords:
            myMaze.addCoordinate(*coord)
    
        myMaze.printMaze()
    
    mazeTest()