Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何打印表格_Python_List - Fatal编程技术网

Python 如何打印表格

Python 如何打印表格,python,list,Python,List,我需要帮助打印表格。 我希望它打印为: Name TotalHours Jenny 34 Luke 28 Mike 20 Joanne 31 Tom 34 但是,这种情况会发生-> 这是我的代码: hrs_list = [[2, 4, 3, 4, 5, 8, 8], [7, 3, 4, 3, 3, 4, 4], [3, 3, 4, 3, 3, 2, 2], [9, 3, 4

我需要帮助打印表格。 我希望它打印为:

Name TotalHours

Jenny   34

Luke    28

Mike    20

Joanne  31

Tom     34 
但是,这种情况会发生->

这是我的代码:

hrs_list = [[2, 4, 3, 4, 5, 8, 8],
              [7, 3, 4, 3, 3, 4, 4],
              [3, 3, 4, 3, 3, 2, 2],
              [9, 3, 4, 7, 3, 4, 1],
              [3, 5, 4, 3, 6, 3, 8]]

employees = ['Jenny', 'Luke', 'Mike', 'Joanne', 'Tom']


def total_hrs_and_salary():
    print("{} {:>15s} {:>15s}".format("Name", "TotalHours", "TotalSalary"))

    for names in employees:
        for lists in hrs_list:
            total = 0
            a = []
            i = 0
            for hrs in lists:
                total = total + hrs

            print("{} {:>15s}".format(names, total))

您可以这样打印:

def total_hrs_and_salary():
    print("name\tTotalHours\n") 
    for i, name in enumerate(employees):
        print(f"{name}\t{sum(hrs_list[i])}\n")

total_hrs_and_salary()
输出:

name    TotalHours

Jenny   34

Luke    28

Mike    20

Joanne  31

Tom     32

您可以这样打印:

def total_hrs_and_salary():
    print("name\tTotalHours\n") 
    for i, name in enumerate(employees):
        print(f"{name}\t{sum(hrs_list[i])}\n")

total_hrs_and_salary()
输出:

name    TotalHours

Jenny   34

Luke    28

Mike    20

Joanne  31

Tom     32

。感谢您在stack overflow中发布,请勿将图像作为文本发布,因为我试图根据屏幕截图理解您的问题您是否能够实现输出?需要一些改进吗?如果您使用
pandas
,请尝试以下操作:
pd.DataFrame({'Name':employees,'TotalHours':[hr\u list中的小时数总和]})
。感谢您在stack overflow中发布,不要将图像作为文本发布,因为我试图根据屏幕截图理解您的问题,您是否能够实现输出?需要改进吗?如果您使用
pandas
,请尝试以下操作:
pd.DataFrame({'Name':employees,'TotalHours':[hrs\u列表中小时的总和])