如何将以下代码转换为2D数组?

时间:2017-02-17 19:30:37

标签: arrays python-3.x formatting

另外,我如何制作所有方括号和" s都已消失,所以看起来像连接4板?

Row7 = [" ","A","B","C","D","E","F","G"]  
Row6 = ["1","-","-","-","-","-","-","-"]  
Row5 = ["2","-","-","-","-","-","-","-"]  
Row4 = ["3","-","-","-","-","-","-","-"]  
Row3 = ["4","-","-","-","-","-","-","-"]  
Row2 = ["5","-","-","-","-","-","-","-"]  
Row1 = ["6","-","-","-","-","-","-","-"]

board = [[Row7], [Row6], [Row5], [Row4], [Row3], [Row2], [Row1]]

print (board)

所以,如果可能的话,我希望它看起来像这样:

A B C D E F G

1 - - - - - - -

2 - - - - - - -

3 - - - - - - -

4 - - - - - - -

5 - - - - - - -

6 - - - - - - -

它也需要是一个2D数组! 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

您可以将该板声明为2d数组(列表列表)

board = [[" ","A","B","C","D","E","F","G"],
         ["1","-","-","-","-","-","-","-"],  
         ["2","-","-","-","-","-","-","-"],  
         ["3","-","-","-","-","-","-","-"],  
         ["4","-","-","-","-","-","-","-"],  
         ["5","-","-","-","-","-","-","-"],  
         ["6","-","-","-","-","-","-","-"]]
#or if you need the rows
# Row7 is an list... so board will be a list of lists (2d array) 
# board = [Row7, Row6, Row5, Row4, Row3, Row2, Row1]

def printB( board ) :
   for row in board: 
      print( " ".join(row) )


printB( board )


>  A B C D E F G
 1 - - - - - - -
 2 - - - - - - -
 3 - - - - - - -
 4 - - - - - - -
 5 - - - - - - -
 6 - - - - - - -