[英]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.