【问题标题】:Adding two lists to the first column and last row of string将两个列表添加到字符串的第一列和最后一行
【发布时间】:2018-06-02 03:12:51
【问题描述】:

我有这个代码:

    def __repr__(self):

    """
    :return: Return a string representation of the board.
    """
    list1=['0','1','2','3','4','5','-']
    list2=['0','1','2','E','4','5']
    output=''
    for row in self.board:

        output=output+str(row)+'\n'


    return output

self.board 是列表列表,输出如下,

这个矩阵的输出是字符串:

['_', '_', '_', 'R', '_', '_']
['_', '_', '_', 'R', '_', '_']
['_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_']
['p', 'p', '_', '_', '_', '_']

现在,正如您在代码中看到的那样,我定义了两个列表,我想将list1 添加为该字符串的第一列,并将list2 添加为最后一行,我仍然无法确定我该怎么做才能做到这一点..

这是我想做的输出:

['0', '_', '_', '_', 'R', '_', '_']
['1', '_', '_', '_', 'R', '_', '_']
['2', '_', '_', '_', '_', '_', '_']
['3', '_', '_', '_', '_', '_', '_']
['4', '_', '_', '_', '_', '_', '_']
['5', 'p', 'p', '_', '_', '_', '_']
['-', '0', '1', '2', 'E', '4', '5']

有什么想法可以更新我的代码以进行管理吗?

谢谢!

【问题讨论】:

    标签: python arrays list class matrix


    【解决方案1】:

    使用enumerate 操作可以获得数组中的索引和对象。

    这样,您可以在打印该行之前打印正确的字符,然后打印最后一行。

    def __repr__(self):
    
        """
        :return: Return a string representation of the board.
        """
        list1 = ['0','1','2','3','4','5']
        list2 = ['-','0','1','2','E','4','5']
        output = ''
        for rowNumber, row in enumerate(self.board):
            output = list1[rowNumber] + output + str(row)+'\n'
        output = output + str(list2)
    
        return output
    

    【讨论】:

    • 但是您正在将列表添加到不正确的字符串中,您在哪里执行此操作,输出 = 输出 + list2
    • 已修复,您可以给我点赞@OdayHajYehia 吗?这对我来说非常重要,我认为我做得很好。
    • 当然,你也帮了很多忙,我为你点赞。
    【解决方案2】:
    def __repr__(self):
    
        """
        :return: Return a string representation of the board.
        """
        list1=['0','1','2','3','4','5','-']
        list2=['0','1','2','E','4','5']
        output=''
        for x in xrange(len(self.board)):
            output = output + str([list1[x]] + self.board[x])+'\n'
        output = output + str([list1[-1] + list2) + '\n'
    
        return output
    

    【讨论】:

    • 感谢这个工作,只需要删除范围 xD 之前的 x
    • 为什么是 range 而不是 xrange?
    • 也许他在 3.x 中
    • 是的,我在 3.x 中不知道它在其他版本中的不同,抱歉
    【解决方案3】:

    如果board正好如下,那么你可以将list2添加到board,压缩board,然后用list1中的值压缩board的每一行:

    board = [['_', '_', '_', 'R', '_', '_'],
    ['_', '_', '_', 'R', '_', '_'],
    ['_', '_', '_', '_', '_', '_'],
    ['_', '_', '_', '_', '_', '_'],
    ['_', '_', '_', '_', '_', '_'],
    ['p', 'p', '_', '_', '_', '_']]
    list1=['0','1','2','3','4','5','-']
    list2=['0','1','2','E','4','5']
    final_board = [[a]+list(c) for a, c in zip(list1, list(zip(board+[list2])))]
    new_final_board = [[a, *b] for a, b in final_board]
    

    输出:

    ['0', '_', '_', '_', 'R', '_', '_']
    ['1', '_', '_', '_', 'R', '_', '_']
    ['2', '_', '_', '_', '_', '_', '_']
    ['3', '_', '_', '_', '_', '_', '_']
    ['4', '_', '_', '_', '_', '_', '_']
    ['5', 'p', 'p', '_', '_', '_', '_']
    ['-', '0', '1', '2', 'E', '4', '5']
    

    【讨论】:

    • 就是这样,但我不想改变板子的大小,这就是我用字符串做的原因。我希望板子的大小保持 6x6
    • @OdayHajYehia 您想要的输出也是 7x7。你能澄清一下吗?
    • 我知道我想要的输出是 7x7,但它是字符串,这意味着我班级规模的主板不会改变,只会改变它的表示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多