简体   繁体   中英

Python issue with lists of lists and printing

Having a problem printing from a list of lists, which reads from a file;

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'), '')

This only prints part of what I want to do, but I've been doing so as a test. what I want it to print as looks like

 1 1 1 No Real Roots
 9 -2 14 No Real Roots
 6 2 10 No Real Roots

What I get after compiling:

 [[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 

Why does it keep adding to the line?

You are appending the whole list every time.

    inputList1.append(input1)
    inputList1.append(input2)
    inputList1.append(input3)
    inputListA.append(inputList1[:]) # ouchy

You probably mean something like this:

    inputListA.append(inputList1[-3:])

Either that or you mean to clear inputListN at each pass.

EDIT:

To get rid of brackets:

    format(' '.join([str(i) for i in currentList]), ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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