Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 以相等字符间距打印二维列表_Python_Nested Lists - Fatal编程技术网

Python 以相等字符间距打印二维列表

Python 以相等字符间距打印二维列表,python,nested-lists,Python,Nested Lists,我正在做一个棋盘由2d数组表示的国际象棋游戏。但是,棋子的宽度与空格不同,因此当我尝试这样打印时,棋子会移位: ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖ ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜ def printBoard2(): b = "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\ "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\

我正在做一个棋盘由2d数组表示的国际象棋游戏。但是,棋子的宽度与空格不同,因此当我尝试这样打印时,棋子会移位:

♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖ 
♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙ 



   ♟           
♟ ♟   ♟ ♟ ♟ ♟ ♟ 
♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
def printBoard2():
    b = "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}".format(*board2)
    return b

board2[36] = p
board[4][3] = T
printBoard()

print("\n\n")

print(printBoard2())
以下是打印功能:

def print_board():
    y = ["♖", "♜", "♗", "♝", "♘", "♞", "♕", "♛", "♔","♚", "♙", "♟"," "]
    for i in range(8):
        tempListe = "".join(board[i])
        z=0
        for x in ["T","t","L","l","H","h","D","d","K","k","P","p","_"]:
            if x in tempListe:
                tempListe = tempListe.replace(x,y[z])
            z+=1
        for j in range(8):
            print("{0:2}".format(tempListe[j]),end="")
        print("")
print_board()
我试过很多不同的方法,但结果似乎都一样。我怎样才能在碎片之间的空间变得太大的情况下修复这个问题

 T,t,L,l,H,h,D,d,K,k,P,p,_= "♖♜♗♝♘♞♕♛♔♚♙♟ "


board = [
[T,L,H,D,K,H,L,T],
[P,P,P,P,P,P,P,P],
[_,_,_,_,_,_,_,_],
[_,_,_,_,_,_,_,_],
[_,_,_,_,_,_,_,_],
[_,_,_,_,_,_,_,_],
[p,p,p,p,p,p,p,p],
[t,l,h,d,k,h,l,t]
]

board2 = [
T,L,H,D,K,H,L,T,
P,P,P,P,P,P,P,P,
_,_,_,_,_,_,_,_,
_,_,_,_,_,_,_,_,
_,_,_,_,_,_,_,_,
_,_,_,_,_,_,_,_,
p,p,p,p,p,p,p,p,
t,l,h,d,k,h,l,t
]

def printBoard():
    for e in board:
        for ee in e:
            print(ee, end='')
        print()
代码可以继续使用另一种布局,如下所示:

♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖ 
♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙ 



   ♟           
♟ ♟   ♟ ♟ ♟ ♟ ♟ 
♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
def printBoard2():
    b = "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}\n"\
        "{:2}{:2}{:2}{:2}{:2}{:2}{:2}{:2}".format(*board2)
    return b

board2[36] = p
board[4][3] = T
printBoard()

print("\n\n")

print(printBoard2())
最后的输出是这样的

第一

♖♗♘♕♔♘♗♖
♙♙♙♙♙♙♙♙


   ♖    

♟♟♟♟♟♟♟♟
♜♝♞♛♚♞♝♜
第二种布局

♖ ♗ ♘ ♕ ♔ ♘ ♗ ♖ 
♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙ 


        ♟       

♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟ 
♜ ♝ ♞ ♛ ♚ ♞ ♝ ♜
在promt上,它看起来是对齐的,但在这里(在html中)没有对齐。 我会把它的图像放在下面


您试图在不使用水平制表符的情况下显示表格数据(因为它会产生相当宽的列)。但是您的输出设备的空格和象棋符号的宽度不同,因此使用空格将不起作用。如果这是在其他单空格文本中,则最有可能的情况是您的显示设备退回到另一种字体,因为主字体缺少棋子,而这种差异将是宽度问题的根源

有一些特殊的宽度,包括数字宽度,但(据我所知)没有为其定义特定的宽度。我的第一个想法是,它们是否可以与一个特别宽的空间组合,但Unicode似乎没有组合空间。这就留下了两个基本选项:使用布局系统来完成这项任务,比如HTML表格,或者使用包含空格和象棋符号的单空格字体。后者最容易在您自己的终端上完成,也可以使用webfont在您自己的网页上请求,但有许多地方您无法控制,包括单独的web浏览器。堆栈溢出尤其不允许表在其标记中,尽管它似乎有一个javascript棋盘渲染器()


另一个(更难看的)解决方法是使用其他格式化功能在空白处渲染棋子,但着色使其不可见。这不适用于像表情符号一样具有自己颜色的字体。它还将对屏幕阅读器造成严重破坏,或复制文本

不能复制。此外,空格的宽度应与任何“适当”控制台字体中任何字符的宽度相同。请确保您的输出没有使用Arial或其他内容。顺便说一句,您可以用
d=dict(zip(“ttllhhdkkkpp”)替换该函数的主体♖♜♗♝♘♞♕♛♔♚♙♟ ")); 对于线路板中的线路:打印(“.join(map(d.get,line)))