如何在不同的行中打印列表

时间:2020-10-18 18:50:57

标签: python

我正在尝试使用列表矩阵为Noughts and Crosses游戏创建一个棋盘,但是我无法以3x3原始和列的顺序排列每个列表,有人可以帮我吗?

''' 
board = [
[ "|_|","|_|","|_|"],
["|_|","|_|","|_|"],
["|_|","|_|","|_|"],  
]

print (board)

'''

2 个答案:

答案 0 :(得分:2)

也许您可以创建一个名为class BooksView(APIView): def get(self, request): books = Book.objects.filter(language__in=request.user.languages.all()) return books 之类的辅助函数,在其中使用str.join打印每一行,并在各行的单元格之间使用制表符:

print_current_board

输出:

def print_current_board(board):
    for row in board:
        print('\t'.join(row))

board = [
    ["|_|", "|_|", "|_|"],
    ["|_|", "|_|", "|_|"],
    ["|_|", "|_|", "|_|"],
]

print_current_board(board)

答案 1 :(得分:0)

#You can use End in Print statement:-
def print_current_board(board):
    for row in board:
        for i in row:
            print(i,end='')
        print('')

board = [
    ["|_|", "|_|", "|_|"],
    ["|_|", "|_|", "|_|"],
    ["|_|", "|_|", "|_|"] ]

print_current_board(board)