说我有一个列表:
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这样的值,而不是零。那样会使木板错位,并使它看起来有些混乱,但是当我得到一堆数字时,木板看起来会很混乱,像这样:
| 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 |
--------------------------
答案 0 :(得分:2)
下面的代码在列表中找到最大数字的长度,然后在每个数字的两边分别添加填充。之后,我们通过遍历列表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 |
--------------------------
答案 1 :(得分:1)
首先计算每列的最大宽度,然后使用字符串格式化程序以该列的最大宽度填充每个数字,并使其居中对齐。行分隔符的宽度就是所有列宽加上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 |
--------------------------