垂直时如何正确对齐?

发布于 2025-02-09 19:59:54 字数 992 浏览 2 评论 0 原文

我有一个在Python中发挥功能的任务,该功能垂直安排了几个算术问题,因此我所需的输出就是这样:

  32         1      9999      523
+  8    - 3801    + 9999    -  49
----    ------    ------    -----
  40     -3800     19998      474

为了产生这样的输出,我写了一个“ for” for tor to the the Cright(参数)此功能的列表是: [“ 32 + 698”,“ 3801-2”,“ 45 + 43”,“ 123 + 49”,“ 555 + 555”] ),将它们分配给变量,然后它应该像所需的输出一样将它们打印出来。要打印出来,我写了这篇文章:

    sol = \
(   f'  {first}'
    f'\n{oper}'
    f' {second}'
    f'\n{dash}'
    f'\n  {sum}')
    lst.append(sol)

{first} 是参数的第一个数字, {oper> {oper} 是操作员, {second} 是第二个数字, {dash} 是可调破折号, {sum} 是算术问题的解决方案。最后一行将垂直算术解决方案附加到列表中,我试图从中水平打印它们。

print(f'{lst[0]}    {lst[1]}    {lst[2]}    {lst[3]}')

但是,我得到的输出是:

  32
+ 698
-----
  730      3801
- 2
------
  3799      45
+ 43
----
  88      123
+ 49
-----
  172

如何将解决方案均匀地制作并正确对齐?

I have a task to make a function in Python, which arranges a couple of arithmetic problems vertically, so my desired output is something like this:

  32         1      9999      523
+  8    - 3801    + 9999    -  49
----    ------    ------    -----
  40     -3800     19998      474

To produce an output like that, I wrote a "for" loop which goes through the argument (argument of this function is a list: ["32 + 698", "3801 - 2", "45 + 43", "123 + 49", "555 + 555"]), assigns them to variables and then it should print them out like in the desired output. To print it out, I wrote this:

    sol = \
(   f'  {first}'
    f'\n{oper}'
    f' {second}'
    f'\n{dash}'
    f'\n  {sum}')
    lst.append(sol)

{first} is the first number from the argument, {oper} is the operator, {second} is the second number, {dash} is the adjustable dashes, {sum} is the solution of the arithmetic problem. The last line appends the vertical arithmetic solutions to a list, from which I try to print them horizontally.

print(f'{lst[0]}    {lst[1]}    {lst[2]}    {lst[3]}')

But then, the output I get is this:

  32
+ 698
-----
  730      3801
- 2
------
  3799      45
+ 43
----
  88      123
+ 49
-----
  172

How to make the strings with solutions even and aligned properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

镜花水月 2025-02-16 19:59:54

这是一个简化的版本,没有列表综合,

problems = ["32 + 698", "3801 - 2", "45 + 43", "123 + 49", "555 + 555"]

width = 5
space = "    "

lst = []
for problem in problems:
    lst.append(problem.split(' '))

for problem in lst:
    print(problem[0].rjust(width), end=space)
print()

for problem in lst:
    print(f"{problem[1]}{problem[2].rjust(width-1)}", end=space)
print()

for problem in lst:
    print("-" * width, end=space)
print()

for problem in problems:
    print(str(eval(problem)).rjust(width), end=space)
print()

可能需要解释的唯一部分是创建此列表的第一个循环:

[['32', '+', '698'], ['3801', '-', '2'], ['45', '+', '43'], ['123', '+', '49'], ['555', '+', '555']]

它将每个问题分解为列表[Operand1,operator,operand2]。


这是第一个(可能过于复杂的)版本。

进行一些预处理,并为每行列出一个列表,然后打印每行。

problems = ["32 + 698", "3801 - 2", "45 + 43", "123 + 49", "555 + 555"]

width = 5

lst = list(zip(*[p.split(' ') for p in problems]))
lines = [[s.rjust(width) for s in lst[0]],
        [f"{op}{val.rjust(width-1)}" for op,val in zip(lst[1], lst[2])],
        ['-' * (width)] * len(lst[0]),
        [str(eval(p)).rjust(width) for p in problems]]

for l in lines: print('   '.join(l))

输出:

   32    3801      45     123     555
+ 698   -   2   +  43   +  49   + 555
-----   -----   -----   -----   -----
  730    3799      88     172    1110

说明:

lst = list(zip(*[p.split('''for p.s in caresears in respare])))

  • 将每个问题分配到列表中。例如:“ 32 + 698” 变为 [“ 32”,“ +”,“ 698”]
  • 然后它将每个部分(操作数1,操作员,操作数2)缩成他们自己的清单。例如: [('32','3801','45','123','555'),('+',' - ' - ','+','+','+'), ('698','2','43','49','555')]

[s.rjust(width)for lst [0]]

  • 创建行一个将每个值设置为固定宽度
  • ex: [“ 32”,“ 3801”,“ 45”,“ 123”,“ 555”]

[f“ {op} {val) 。

  • ​“,” - 2“,”+ 43“,”+ 49“,”+ 555“]

[' - ' *(width)] * len(lst [0]),

  • 创建破折号的第三行

[str(eval(p))。问题中的p的rjust(width)]

  • 最后一行,所有总和在正确的宽度上

Here's a simplified version with no list comprehensions

problems = ["32 + 698", "3801 - 2", "45 + 43", "123 + 49", "555 + 555"]

width = 5
space = "    "

lst = []
for problem in problems:
    lst.append(problem.split(' '))

for problem in lst:
    print(problem[0].rjust(width), end=space)
print()

for problem in lst:
    print(f"{problem[1]}{problem[2].rjust(width-1)}", end=space)
print()

for problem in lst:
    print("-" * width, end=space)
print()

for problem in problems:
    print(str(eval(problem)).rjust(width), end=space)
print()

The only part that might need explaining is the first for loop that creates this list:

[['32', '+', '698'], ['3801', '-', '2'], ['45', '+', '43'], ['123', '+', '49'], ['555', '+', '555']]

It has broken up each problem into a list [operand1, operator, operand2].


Here's the first (possible overly complex) version.

Do some pre-processing and make a list for each line then print each line.

problems = ["32 + 698", "3801 - 2", "45 + 43", "123 + 49", "555 + 555"]

width = 5

lst = list(zip(*[p.split(' ') for p in problems]))
lines = [[s.rjust(width) for s in lst[0]],
        [f"{op}{val.rjust(width-1)}" for op,val in zip(lst[1], lst[2])],
        ['-' * (width)] * len(lst[0]),
        [str(eval(p)).rjust(width) for p in problems]]

for l in lines: print('   '.join(l))

Output:

   32    3801      45     123     555
+ 698   -   2   +  43   +  49   + 555
-----   -----   -----   -----   -----
  730    3799      88     172    1110

Explanation:

lst = list(zip(*[p.split(' ') for p in problems]))

  • Splits each problem into a list. Ex: "32 + 698" becomes ["32", "+", "698"]
  • Then it zips each part (operand 1, operator, operand 2) into their own lists. Ex: [('32', '3801', '45', '123', '555'), ('+', '-', '+', '+', '+'), ('698', '2', '43', '49', '555')]

[s.rjust(width) for s in lst[0]]

  • Creates line one with each value set to a fixed width
  • Ex: [" 32", " 3801", " 45", " 123", " 555"]

[f"{op}{val.rjust(width-1)}" for op,val in zip(lst[1], lst[2])]

  • Line 2. Joins the operators and the 2nd operands
  • Ex: ["+ 698", "- 2", "+ 43", "+ 49", "+ 555"]

['-' * (width)] * len(lst[0]),

  • Creates the 3rd line of dashes

[str(eval(p)).rjust(width) for p in problems]

  • Last line with all the sums at the correct width
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文