Skip to main content
 首页 » 编程设计

python之修复 Python 中的错误

2025年05月04日10mayingbao

我有一个 list ,

L = [['First', 'Last', 'GPA', 'Major', 'Drops'], 
['Jane', 'Doe', '3.5', 'CS', '2'], ['Joe', 'Doe', '2.0', 'CpE', '0'], 
['Todd', 'Brown', '3.88', 'CS', '5'],  
['Mike', 'Smith', '3.88', 'CS', '5']] 

我必须将其打印在表格中,例如:

    -------------------------------------------------------- 
    |     First|      Last|       GPA|     Major|     Drops| 
    -------------------------------------------------------- 
    |      Jane|       Doe|      3.50|        CS|         2| 
    |       Joe|       Doe|      2.00|       CpE|         0| 
    |      Todd|     Brown|      3.88|        CS|         5| 
    |      Mike|     Smith|      3.88|        CS|         5| 

到目前为止我的代码是:

    L = [['First', 'Last', 'GPA', 'Major', 'Drops'], 
     ['Jane', 'Doe', '3.5', 'CS', '2'], 
     ['Joe', 'Doe', '2.0', 'CpE', '0'], 
     ['Todd', 'Brown', '3.88', 'CS', '5'], 
     ['Mike', 'Smith', '3.88', 'CS', '5']] 
count1 = 1 
while count1 < len(L): 
    L[count1][2] = float(L[count1][2]) 
    L[count1][4] = int(L[count1][4]) 
    count1 += 1 
h_line = 56 * '-' 
first_line = "|" 
print (h_line) 
s = " " 
w = 10 
for i in range(len(L[0])): 
    str1 = (s*(w - len(L[0][i])) + "%s|" % L[0][i]) 
    first_line = first_line + str1 
print(first_line) 
print(h_line) 
a = 1 
while a < len(L): 
    second_line = "|" 
    for j in range (len(L[a])): 
        if type(L[a][j]) == str : 
            str2 = (s*(w - len(L[a][j])) + ("%s|" % L[a][j])) 
            second_line = second_line + str2 
        elif type(L[a][j]) == float : 
            str2 = (s*(w-4) + ("%.2f|" % L[a][j])) 
            second_line = second_line + str2 
        elif type(L[a][j]) == float : 
            str2 = (s*(w-1) + ("%i|" % L[a][j])) 
            second_line = second_line + str2 
    print (second_line) 
    a = a + 1 
 
 
print (h_line) 

但我的输出看起来像:

    -------------------------------------------------------- 
    |     First|      Last|       GPA|     Major|     Drops| 
    -------------------------------------------------------- 
    |      Jane|       Doe|      3.50|        CS| 
    |       Joe|       Doe|      2.00|       CpE| 
    |      Todd|     Brown|      3.88|        CS| 
    |      Mike|     Smith|      3.88|        CS| 
    -------------------------------------------------------- 

我不明白我做错了什么。伙计们,帮帮我!

请您参考如下方法:

天哪,这是错误的解决方法!!!使用 string formatting

L = [['First', 'Last', 'GPA', 'Major', 'Drops'], 
     ['Jane', 'Doe', '3.5', 'CS', '2'], ['Joe', 'Doe', '2.0', 'CpE', '0'], 
     ['Todd', 'Brown', '3.88', 'CS', '5'],  
     ['Mike', 'Smith', '3.88', 'CS', '5']] 
 
def display_table(rows): 
    template = "|{:>10}|{:>10}|{:>10}|{:>10}|{:>10}|" 
    horiz_rule = "-" * 56 
    header = rows[0] 
    print(horiz_rule) 
    print(template.format(*header)) 
    print(horiz_rule) 
    for row in rows[1:]: 
        print(template.format(*row)) 
    print(horiz_rule) 
 
display_table(L) 

如果您需要能够以编程方式分配列宽,您可以使用额外的 { 和 } 来转义外部格式,例如

template = "|{{:>{0}}}".format(some_width) * num_columns + "|" 
## if some_width is 10 and num_columns is 5,  
## results in the same template as above. Then you can do: 
horiz_rule = 1 + some_width * (num_columns + 1) 

作为一个工作示例,也许您想将每列扩展到至少 10 个空格,但又想对齐到该列中最长的元素加 1。

def display_table(rows): 
    # might want a sanity check here to make sure the table is square 
    num_columns = len(rows) 
    template = "|{{:>{}}}" * len(rows[0]) + "|" 
    header = rows[0] 
    # zip(*iterable) is a good recipe for aligning columnwise 
    column_lengths = [max(10, max(map(len, col)) + 1) for col in zip(*rows)] 
    finished_template = template.format(*column_lengths) 
    hr = "-" * (sum(column_lengths) + num_columns + 1) 
    print(hr) 
    print(finished_template.format(*header)) 
    print(hr) 
    for row in rows[1:]: 
        print(finished_template.format(*row)) 
    print(hr) 
display_table(L) 

结果:

-------------------------------------------------------- 
|     First|      Last|       GPA|     Major|     Drops| 
-------------------------------------------------------- 
|      Jane|       Doe|       3.5|        CS|         2| 
|       Joe|       Doe|       2.0|       CpE|         0| 
|      Todd|     Brown|      3.88|        CS|         5| 
|      Mike|     Smith|      3.88|        CS|         5| 
-------------------------------------------------------- 

或者,如果您添加姓氏为“SomeReallyLongName”的学生:

L.append(['Foo','SomeReallyLongName','2.0','Mus','10']) 
display_table(L) 
## OUTPUT 
------------------------------------------------------------------ 
|     First|               Last|       GPA|     Major|     Drops| 
------------------------------------------------------------------ 
|      Jane|                Doe|       3.5|        CS|         2| 
|       Joe|                Doe|       2.0|       CpE|         0| 
|      Todd|              Brown|      3.88|        CS|         5| 
|      Mike|              Smith|      3.88|        CS|         5| 
|       Foo| SomeReallyLongName|       2.0|       Mus|        10| 
------------------------------------------------------------------ 

看起来最后一位在计算水平规则时有一个小差错。它对我来说看起来不错,但显然它关闭了(一个!)我将把它压扁作为读者的练习。