2013-12-08 5 views
1

Ошибка при печати из списка списков, который читается из файла;Проблема с Python со списками списков и печать

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 

Почему продолжать добавлять к линии?

ответ

0

Вы добавляете весь список каждый раз.

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

Вы, наверное, имели в виду что-то вроде этого:

inputListA.append(inputList1[-3:]) 

Либо это, либо вы имеете в виду, чтобы очистить inputListN при каждом проходе.

EDIT:

Чтобы избавиться от скобок:

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

Отлично! Мне просто нужно избавиться от скобок вокруг списка, когда он печатает сейчас. – user3055517

Смежные вопросы