我如何格式化一些数字并使 space 彼此相等?

How can I format some numbers and have the space that is taken equally to each other?

我的代码看起来像这样:

plants = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"]
Grid_Of_Plants = """
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
""".format(plants[0],plants[1],plants[2],plants[3],plants[4],plants[5],plants[6],plants[7],plants[8],plants[9],plants[10],plants[11],plants[12],plants[13],plants[14],plants[15],plants[16],plants[17],plants[18],plants[19],plants[20],plants[21],plants[22],plants[23],plants[24])
print(Grid_Of_Plants)

如果我运行它,它会变成这样:

|1|2|3|4|5|
|6|7|8|9|10|
|11|12|13|14|15|
|16|17|18|19|20|
|21|22|23|24|25|

如您所见,只有一位数字的 space 没有正确对齐。 我该如何解决?

如果在plants变量下定义右键,添加:

plants = [i.rjust(2) for i in plants]

输出:


| 1| 2| 3| 4| 5|
| 6| 7| 8| 9|10|
|11|12|13|14|15|
|16|17|18|19|20|
|21|22|23|24|25|

如果可能有 three-digit 个数字、four-digit 个数字等等,您可以像这样动态地使用它作为填充:

pad = max(map(len, plants))
plants = [i.rjust(pad) for i in plants]

这是一个选项,使用基础 f-string 格式 f"{item:>2f}"s 字符串保留 2 个空格; right-aligned (>):

max_pad = max(len(s) for s in plants)  # get the maximal space needed
it = iter(plants)
for line_nr in range(5):
    line = "|".join(f"{next(it):>{max_pad}s}" for _ in range(5))
    print(f"|{line}|")

它打印:

| 1| 2| 3| 4| 5|
| 6| 7| 8| 9|10|
|11|12|13|14|15|
|16|17|18|19|20|
|21|22|23|24|25|

使用更多的迭代器魔法来从列表中获取行,您可以这样做:

max_pad = max(len(s) for s in plants)
items_per_line = 5
lines = zip(*([iter(plants)] * items_per_line))
for line in lines:
    line_str = "|".join(f"{item:>{max_pad}s}" for item in line)
    print(f"|{line_str}|")

lines 是一个迭代器,当您对其进行迭代时,它将 return tupleitems_per_line 元素。

(其中一些是从 itertools recipes 中的 grouper 借来的)

您可以填充您的输入(参见 ),或者您可以根据您的数字中存在的最大宽度格式化单数单元格:

plants = [str(n) for n in range (1,26)]    # shortcut for yours

w = max(len(k) for k in plants)
Grid_Of_Plants = """
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
""".replace("{}",f"{{:>{w}}}")   # create the "template" with formatting

print(Grid_Of_Plants)

filled = Grid_Of_Plants.format(*plants)    # shortcut for yours
print(filled)

输出:

# template
|{:>2}|{:>2}|{:>2}|{:>2}|{:>2}|
|{:>2}|{:>2}|{:>2}|{:>2}|{:>2}|
|{:>2}|{:>2}|{:>2}|{:>2}|{:>2}|
|{:>2}|{:>2}|{:>2}|{:>2}|{:>2}|
|{:>2}|{:>2}|{:>2}|{:>2}|{:>2}|

# filled
| 1| 2| 3| 4| 5|
| 6| 7| 8| 9|10|
|11|12|13|14|15|
|16|17|18|19|20|
|21|22|23|24|25|

这使用 Format Specification Mini-Language:>2 右对齐到 2 个字符。如果您使用数字而不是字符串,则可以省略 > 以及右对齐是默认设置:

print("""|{:2}|{:2}|{:2}|{:2}|{:2}|
|{:2}|{:2}|{:2}|{:2}|{:2}|
|{:2}|{:2}|{:2}|{:2}|{:2}|
|{:2}|{:2}|{:2}|{:2}|{:2}|
|{:2}|{:2}|{:2}|{:2}|{:2}|""".format(*range(1,26)))

相同的输出。

如果 table 的大小不会改变,这似乎是实现预期结果所需的最小改变。

plants = [" 1"," 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"]
Grid_Of_Plants = """
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
""".format(plants[0],plants[1],plants[2],plants[3],plants[4],plants[5],plants[6],plants[7],plants[8],plants[9],plants[10],plants[11],plants[12],plants[13],plants[14],plants[15],plants[16],plants[17],plants[18],plants[19],plants[20],plants[21],plants[22],plants[23],plants[24])
print(Grid_Of_Plants)

我只是在 plants 数组中的单个数字的开头添加了一个 space。

这是结果:

| 1| 2| 3| 4| 5|
| 6| 7| 8| 9|10|
|11|12|13|14|15|
|16|17|18|19|20|
|21|22|23|24|25|

我们可以在使用切片取最后 2 个字符之前添加 space。

plants = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"]
for i in range(0,len(plants)):
    plants[i] = (' ' + plants[i])[-2:]
Grid_Of_Plants = """
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
|{}|{}|{}|{}|{}|
""".format(plants[0],plants[1],plants[2],plants[3],plants[4],plants[5],plants[6],plants[7],plants[8],plants[9],plants[10],plants[11],plants[12],plants[13],plants[14],plants[15],plants[16],plants[17],plants[18],plants[19],plants[20],plants[21],plants[22],plants[23],plants[24])
print(Grid_Of_Plants)
| 1| 2| 3| 4| 5|
| 6| 7| 8| 9|10|
|11|12|13|14|15|
|16|17|18|19|20|
|21|22|23|24|25|