gpt4 book ai didi

python - 如何在python 3.6中整齐地打印一长串数字?

转载 作者:太空宇宙 更新时间:2023-11-03 12:26:04 25 4
gpt4 key购买 nike

>>> text = [ str(i) for i in range(1, 100)]
>>> print( " {}".format( ", ".join( str(i) for i in text ) ) )
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, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99

我不想打印长文本字符串,而是希望打印出每行 80 个字符宽度的 text 更整洁,每行缩进 2 个字符空间,每个元素等距像这样:

   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, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99

是否有现成的 python 对象可用于执行此操作?如果没有,在 python 3.6 中实现这个的 pythonic 方法是什么?

最佳答案

正如 Saelyth 指出的那样,使用 textwrap 来格式化它,但在此之前,您需要将每个数字格式化为右对齐并占用 2 个空格:

import textwrap

text = [str(i) for i in range(1, 100)]
one_line = ', '.join('{:>2}'.format(e) for e in text)
# one_line == ' 1, 2, 3, ...'
print('\n'.join(textwrap.wrap(one_line, width=80)))

输出:

 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, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99

更新

如果你想要 2 个空格的缩进,第一种方法是用缩进打印每一行:

import textwrap

text = [str(i) for i in range(1, 100)]
one_line = ', '.join('{:>2}'.format(e) for e in text)
# one_line == ' 1, 2, 3, ...'
for line in textwrap.wrap(one_line, width=78):
print(' {}'.format(line))

第二种方法是使用textwrap.indent:

import textwrap

text = [str(i) for i in range(1, 100)]
one_line = ', '.join('{:>2}'.format(e) for e in text)
# one_line == ' 1, 2, 3, ...'
block = '\n'.join(textwrap.wrap(one_line, width=78))
print(textwrap.indent(block, ' '))

请注意,由于 2 个空格的缩进,我将宽度从 80 减少到 78。

更新2

感谢 Tomerikoo 建议使用 textwrap.fill,代码现在更简单了:

import textwrap

text = [str(i) for i in range(1, 100)]
one_line = ', '.join('{:>2}'.format(e) for e in text)
# one_line == ' 1, 2, 3, ...'

print(textwrap.fill(one_line,
width=80,
initial_indent=' ',
subsequent_indent=' '))

更新 3

在此更新中,我添加了 Tim 的建议以进一步减少步骤。

import textwrap

one_line = ', '.join('{:>2}'.format(e) for e in range(1, 100))
# one_line == ' 1, 2, 3, ...'

print(textwrap.fill(one_line,
width=80,
initial_indent=' ',
subsequent_indent=' '))

关于python - 如何在python 3.6中整齐地打印一长串数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57237034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com