我这里有一些代码:
for i in range(self.size):
print('{:6d}'.format(self.data[i], end=' '))
if (i + 1) % NUMBER_OF_COLUMNS == 0:
print()
现在这打印为:
1 1 1 1 1 2 3 3 3 3 (whitespace) 3 3 3 etc.
它在达到 10 位数字时创建一个新行,但它不会连续打印最初的 10 位...
这就是我要的-
1 1 1 1 1 1 1 2 2 3
3 3 3 3 3 4 4 4 4 5
然而,当它达到两位数时,它就搞砸了——
8 8 8 8 8 9 9 9 9 10
10 10 10 10 10 10 etc.
我希望它像这样右对齐-
8 8 8 8 8 9
10 10 10 10 11 12 etc.
当我删除格式块时,它会打印出行,但当然不会有额外的间距!
您可以使用字符串的.rjust
方法通过“填充”值来对齐字符串。使用一些虚拟数据:
NUMBER_OF_COLUMNS = 10
for i in range(100):
print("{}".format(i//2).rjust(3), end=' ')
#print("{:3}".format(i//2), end=' ') edit: this also works. Thanks AChampion
if (i + 1) % NUMBER_OF_COLUMNS == 0:
print()
#Output:
0 0 1 1 2 2 3 3 4 4
5 5 6 6 7 7 8 8 9 9
10 10 11 11 12 12 13 13 14 14
15 15 16 16 17 17 18 18 19 19
20 20 21 21 22 22 23 23 24 24
25 25 26 26 27 27 28 28 29 29
30 30 31 31 32 32 33 33 34 34
35 35 36 36 37 37 38 38 39 39
40 40 41 41 42 42 43 43 44 44
45 45 46 46 47 47 48 48 49 49
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句