繁体   English   中英

在 python 的同一行上打印格式化列表项

[英]printing formatted list items on the same line in python

我是一名初学者程序员,正在完成 freecodecamp 上的最终项目之一。

我正在使用 Mac OS python3.10

该问题要求我们创建一个函数,该函数将水平排列的算术问题列表作为参数并垂直重新排列它们。

这是 function 调用:

arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])

这是期望的结果:

   32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----

我能够垂直重新格式化方程,但我被困在试图弄清楚如何在同一行上并排打印它们。 这是我写的代码。

def aa(problem) :
    for i in problem[:] :
        problem.remove(i)
        p = i.split()
        # print(p)
        ve = '\t{:>5}\n\t{:<1}{:>4}\n\t-----'.format(p[0],p[1],p[2])
        print(ve)

    return

aa(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])

这是该代码的结果。

   32
+ 698
-----
 3801
-   2
-----
   45
+  43
-----
  123
+  49
-----

我已经尝试过使用 print(*variable) 和 ''.join。 当我尝试这些解决方案时,我得到了这个。

       3 2
 +   6 9 8
 - - - - -
   3 8 0 1
 -       2
 - - - - -
       4 5
 +     4 3
 - - - - -
     1 2 3
 +     4 9
 - - - - -

感谢您花时间阅读我的问题,并感谢您的帮助。

当您将字符打印到终端时,cursor position 会向前移动。 当您打印换行符时,cursor 会下降一个 position。 您必须手动再次启动它才能在上面的行中打印。 您可以使用ANSI 转义码来控制 cursor 的 position。 它更加困难和复杂。

您可以通过更改方程式的表示方式来实现所需的 output。 将每个方程存储为[operand1, sign, operand2] 现在,只需在一行中打印所有操作数 1。 接下来打印符号和操作数 2。 然后打印-----

def fmt(lst):
    op1, sign, op2 = zip(*map(str.split, lst))
    line1  = "\t".join([f"{op:>5}" for op in op1])
    line2  = "\t".join([f"{s:<1}{op:>4}" for s, op in zip(sign, op2)])
    line3  = "-----\t"*len(lst)
    print("\n".join([line1, line2, line3])

fmt(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])

Output:

   32    3801      45     123
+ 698   -   2   +  43   +  49
-----   -----   -----   -----   

感谢所有花时间提供帮助的人。 我使用@MarkTolonen 的提示提出了一个解决方案,他建议我将列表放入列表中。 下面是我的解决方案。 让我知道是否有更优雅的方法来做到这一点。

Function:

def aa(problem) :
    new_list = list()
    for i in problem[:] :
        problem.remove(i)
        p = i.split()
        new_list.append(p)
    print(new_list)
    for l in new_list :
        l1 = '{:>5}'.format(l[:][0])
        print(l1,end='   ')
    print('')
    for l in new_list:
        l2 = '{:<1}{:>4}'.format(l[:][1],l[:][2])
        print(l2,end='   ')
    print('')
    for l in new_list:
        print('-----',end='   ')

    return

Function 致电:

aa(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])

结果:

   32    3801      45     123
+ 698   -   2   +  43   +  49
-----   -----   -----   -----   %

还。 打印破折号末尾的“%”是什么意思?

这是一个与预期结果完全匹配的解决方案。 它决定了每个方程的宽度:

def arithmetic_arranger(equations):
    # Parse to list of lists, e.g.:
    #   [['32', '+', '698'], ['3801', '-', '2'], ['45', '+', '43'], ['123', '+', '49']]
    parsed = [[item for item in equation.split()] for equation in equations]

    # For each sublist, determine the widest string and build a list of those widths, e.g.:
    #   [3, 4, 2, 3]
    widths = [len(max(items,key=len)) for items in parsed]

    # zip() matches each width with each parsed sublist.
    # Print the first operands right-justified with appropriate widths.
    print('   '.join([f'  {a:>{w}}' for w,(a,op,b) in zip(widths,parsed)]))

    # Print the operator and the second operand.
    print('   '.join([f'{op} {b:>{w}}' for w,(a,op,b) in zip(widths,parsed)]))

    # Print the dashed lines under each equation.
    print('   '.join(['-'*(w+2) for w in widths]))

arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])

Output:

   32     3801     45     123
+ 698   -    2   + 43   +  49
-----   ------   ----   -----

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM