我想为控制台编程国际象棋。
为此,我创建了一个二维数组:
chessboard= [["a1","a2","a3","a4","a5","a6","a7","a8"],["b1","b2","b3","b4","b5","b6","b7","b8"],["c1","c2","c3","c4","c5","c6","c7","c8"],
["d1","d2","d3","d4","d5","d6","d7","d8"],["e1","e2","e3","e4","e5","e6","e7","e8"],["f1","f2","f3","f4","f5","f6","f7","f8"],
["g1","g2","g3","g4","g5","g6","g7","g8"],["h1","h2","h3","h4","h5","h6","h7","h8"]]
我用不同的内容填充它,而我的问题是输出:
-['1', '2', '3', '4', '5', '6', '7', '8']-
1['♖', '♘', '♗', '♔', '♕', '♗', '♘', '♖']1
2['♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙']2
3['⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛']3
4['⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜']4
5['⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛']5
6['⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜']6
7['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟']7
8['♜', '♞', '♝', '♚', '♛', '♝', '♞', '♜']8
-['1', '2', '3', '4', '5', '6', '7', '8']-
没有逗号和撇号,输出将看起来非常完美。 但是我可以将其删除还是有其他方法吗?
编辑: 谢谢你,我的棋盘现在看起来不错了:)
答案 0 :(得分:1)
由于您具有嵌套列表,因此必须遍历每个子列表以将其从列表格式中删除
for i in chessboard:
for x in i:
print(x, end = " ")
print("")
a1 a2 a3 a4 a5 a6 a7 a8
b1 b2 b3 b4 b5 b6 b7 b8
c1 c2 c3 c4 c5 c6 c7 c8
d1 d2 d3 d4 d5 d6 d7 d8
e1 e2 e3 e4 e5 e6 e7 e8
f1 f2 f3 f4 f5 f6 f7 f8
g1 g2 g3 g4 g5 g6 g7 g8
h1 h2 h3 h4 h5 h6 h7 h8
以及执行此操作的pythonic方法:
for i in chessboard: print(*i)
答案 1 :(得分:0)
您可以打印并解包:
for lst in chessboard:
print(*lst)
a1 a2 a3 a4 a5 a6 a7 a8
b1 b2 b3 b4 b5 b6 b7 b8
c1 c2 c3 c4 c5 c6 c7 c8
d1 d2 d3 d4 d5 d6 d7 d8
e1 e2 e3 e4 e5 e6 e7 e8
f1 f2 f3 f4 f5 f6 f7 f8
g1 g2 g3 g4 g5 g6 g7 g8
h1 h2 h3 h4 h5 h6 h7 h8