[英]Python issue with lists of lists and printing
從文件列表讀取的列表列表打印時遇到問題;
input1,input2,input3 = eval(input())
inputList1 = []
inputList2 = []
inputList3 = []
inputListA = []
inputListB = []
inputListC = []
rootList1 = []
rootList2 = []
print(format('Coefficients','15s'),format('# of Roots','15s'),'Roots')
print('==================================================')
while (input1 != 0 and input2 != 0 and input3 != 0):
rootProc = QuadEq(input1,input2,input3)
rootS = rootProc.discRoot()
if (rootS == 0):
inputList2.append(input1)
inputList2.append(input2)
inputList2.append(input3)
inputListB.append(inputList2[:])
rootList1.append(rootProc.RootOne())
elif (rootS > 0):
inputList3.append(input1)
inputList3.append(input2)
inputList3.append(input3)
inputListC.append(inputList3[:])
rootList2.append(rootProc.RootOne())
rootList2.append(rootProc.RootTwo())
else:
inputList1.append(input1)
inputList1.append(input2)
inputList1.append(input3)
inputListA.append(inputList1[:])
input1,input2,input3 = eval(input())
for i in range(len(inputListA)):
print(format(inputListA,'5s'), format('No Real Roots','>15s'), '')
這只是打印了我想做的一部分,但是我一直在做這樣的測試。 我想要它打印的樣子
1 1 1 No Real Roots
9 -2 14 No Real Roots
6 2 10 No Real Roots
編譯后得到的結果是:
[[1, 1, 1], [1, 1, 1, 9, -2, 14], [1, 1, 1, 9, -2, 14, 6, 2, 10]] No Real Roots
[[1, 1, 1], [1, 1, 1, 9, -2, 14], [1, 1, 1, 9, -2, 14, 6, 2, 10]] No Real Roots
[[1, 1, 1], [1, 1, 1, 9, -2, 14], [1, 1, 1, 9, -2, 14, 6, 2, 10]] No Real Roots
為什么它會不斷增加?
您每次都附加整個列表。
inputList1.append(input1)
inputList1.append(input2)
inputList1.append(input3)
inputListA.append(inputList1[:]) # ouchy
您可能的意思是這樣的:
inputListA.append(inputList1[-3:])
或者是您打算在每次通過時清除inputListN。
編輯:
擺脫括號:
format(' '.join([str(i) for i in currentList]), ...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.