Python-3.x:如何在相等的列中打印多个列表?

时间:2018-12-07 13:02:24

标签: python python-3.x list

我对python还是很陌生,我试图在列中输出多个列表,一个在另一个列表下,我不知道。

我当前的输出是:

Name, Count, Correct, Incorrect, Accuracy, Total

Abena 3 3 0 100 45

Malcolm 1 1 0 100 1

Jane 1 1 0 100 20

Liz 1 1 0 100 10

Andy 1 0 1 0 20

Sandip 0 0 0 0 0

如何将数字分成不同的列?

当前要打印的代码为:

CoinFile = open("CoinCount.txt", "r")
    print("Name, Count, Correct, Incorrect, Accuracy, Total")
    for record in VolunteersToSort:
            print(record[0] + " " + record[1]+ " " + record[2] + " " + record[3] + " " + record[4] + " " + record[5] )

文件内容:

['Abena', '3', '3', '0', '100', '45']
['Malcolm', '1', '1', '0', '100', '1']
['Jane', '1', '1', '0', '100', '20']
['Andy', '1', '0', '1', '0', '20']
['Sandip', '0', '0', '0', '0', '0']
['Liz', '1', '1', '0', '100', '10']

2 个答案:

答案 0 :(得分:1)

$_productCollection->setOrder('article_position', 'DESC');
echo '<pre>';
print_r($_productCollection->getdata());

您将输出为:

header = ['Name', 'Count', 'Correct', 'Incorrect', 'Accuracy', 'Total']
my_list = [
    ['Abena', '3', '3', '0', '100', '45'], 
    ['Malcolm', '1', '1', '0', '100', '1'],
    ['Jane', '1', '1', '0', '100', '20'],
    ['Andy', '1', '0', '1', '0', '20'],
    ['Sandip', '0', '0', '0', '0', '0'], 
    ['Liz', '1', '1', '0', '100', '10']
]    
print("{: >10} {: >10} {: >10} {: >10} {: >10} {: >10}".format(*header))
for row in my_list:
    print("{: >10} {: >10} {: >10} {: >10} {: >10} {: >10}".format(*row))

您可以根据需要编辑列长。这里使用10。

答案 1 :(得分:0)

如果要从文件中读取逗号分隔的数据。您可以使用以下代码。

with open("file.txt") as x :
    for w in x :
        print(" ".join(["{:10s}".format(i) for i in w.rstrip('\n').split(',')]))

输出:

Name       Count      Correct    Incorrect  Accuracy   Total     
Abena      3          3          0          100        45        
Malcolm    1          1          0          100        1         
Jane       1          1          0          100        20        
Andy       1          0          1          0          20        
Sandip     0          0          0          0          0         
Liz        1          1          0          100        10