假设我有一个列表:
nlist = [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]
我有一些代码:
def drawBoard():
count = 0
for i in range(16):
print('|',nlist[i], end = ' ')
count += 1
if count == 4:
print("|\n-----------------")
count = 0
print("")下面的代码显示的列表如下:
| 0 | 0 | 0 | 0 |
-----------------
| 0 | 0 | 0 | 0 |
-----------------
| 0 | 0 | 0 | 0 |
-----------------
| 0 | 0 | 0 | 0 |
-----------------现在板子看起来很好,但是假设我的值是128,而不是0中的1。这会使棋盘不对齐,看起来有点乱,但当我得到一大堆数字时,棋盘就会看起来像这样:
| 128 | 0 | 4096 | 0 |
-----------------
| 64 | 0 | 0 | 128 |
-----------------
| 2048 | 0 | 0 | 8192 |
-----------------
| 0 | 0 | 64 | 0 |
-----------------现在,黑板看起来乱七八糟,我在每个数字下面的行数都达不到末尾。我怎样才能让棋盘看起来像这样(也许可以改变直线之间的间距),让它看起来不再凌乱:
| 128 | 0 | 4096 | 0 |
--------------------------
| 64 | 0 | 0 | 128 |
--------------------------
| 2048 | 0 | 0 | 4096 |
--------------------------
| 0 | 0 | 64 | 0 |
--------------------------发布于 2018-07-18 09:12:07
首先计算每列的最大宽度,然后使用字符串格式化程序以该列的最大宽度填充每个数字,并居中对齐。行分隔符的宽度正好是所有列宽加上5个列分隔符的总和。
nlist = [128,0,4096,0, 64,0,0,128, 2048,0,0,8192, 0,0,64,0]
def drawBoard():
widths = [max(len(str(nlist[row * 4 + col])) for row in range(4)) + 2 for col in range(4)]
width = sum(widths) + 5
count = 0
for i in range(16):
print('|{:^{width}}'.format(nlist[i], width=widths[i % 4]), end = '')
count += 1
if count == 4:
print("|\n" + '-' * width)
count = 0
print("")
drawBoard()这将输出以下内容:
| 128 | 0 | 4096 | 0 |
--------------------------
| 64 | 0 | 0 | 128 |
--------------------------
| 2048 | 0 | 0 | 8192 |
--------------------------
| 0 | 0 | 64 | 0 |
--------------------------发布于 2018-07-18 09:11:24
下面的代码找到列表中最大数字的长度,然后相应地在每个数字的边上添加填充。在此之后,我们通过一次迭代列表4步来打印每一行。
在:
L = [128,0,4096,0,64,0,0,128,2048,0,0,4096,0,0,64,0]
m = len(str(max(L))) + 2
L = [ str(x).center(m,' ') for x in L]
step = 4
for i in range(0,len(L),step):
s = '|' + '|'.join(L[i:i+step]) + '|'
print(s)
print('-'*len(s))输出:
| 128 | 0 | 4096 | 0 |
-----------------------------
| 64 | 0 | 0 | 128 |
-----------------------------
| 2048 | 0 | 0 | 4096 |
-----------------------------
| 0 | 0 | 64 | 0 |
-----------------------------如果您想要更大或更小的表,只需更改步骤的值。
在:
L = [128,0,4096,0,64,0,0,128,2048,0,0,4096,0,0,64,0]
m = len(str(max(L))) + 2
L = [ str(x).center(m,' ') for x in L]
step = 8
for i in range(0,len(L),step):
s = '|' + '|'.join(L[i:i+step]) + '|'
print(s)
print('-'*len(s))输出:
| 128 | 0 | 4096 | 0 | 64 | 0 | 0 | 128 |
---------------------------------------------------------
| 2048 | 0 | 0 | 4096 | 0 | 0 | 64 | 0 |
---------------------------------------------------------但是,如果您希望每一列的填充都尽可能紧凑,而又不让人觉得难看,事情就会变得有点复杂。
在:
L = [128,0,4096,0,64,0,0,128,2048,0,0,4096,0,0,64,0]
step = 4
# We calculate the padding for every column and store it in sz[].
# The padding for a column is the same as the minimum padding for its largest element.
sz = []
for index,item in enumerate(L):
column = [L[i] for i in range(index,len(L),step)]
padding = len( str( max(column) ) ) + 2
sz.append(padding)
if index == step + 1:
break
# Now we iterate over the columns ( not linearly over the array )
# and we apply the appropriate padding we have just found out.
i = 0
while i < step + 1:
for j in range(i,len(L),step):
L[j] = str(L[j]).center(sz[i],' ')
i += 1
# Now simply iterate linearly over the array and print a new line
# every step.
for i in range(0,len(L),step):
s = '|' + '|'.join(L[i:i+step]) + '|'
print(s)
print('-'*len(s))输出:
| 128 | 0 | 4096 | 0 |
--------------------------
| 64 | 0 | 0 | 128 |
--------------------------
| 2048 | 0 | 0 | 4096 |
--------------------------
| 0 | 0 | 64 | 0 |
--------------------------https://stackoverflow.com/questions/51392020
复制相似问题