将Python 3.6.5列表转换为表

我目前正在一个项目上,我需要将列表转换为表格的帮助。我已经想出了一种方法,但是必须有一种更短,更清洁的方法来解决这个问题,而我在弄清楚这一点时遇到了麻烦。我的编码如下(我需要帮助的部分在**的底部)


def main():


# Create a list for the sales person    

    sales_persons_list = inputSalesPeople()

    sales_amounts_list = inputSalesAmounts(sales_persons_list)

    displaySalesReport(sales_persons_list, sales_amounts_list)


# Create a list for the sales person

def inputSalesPeople():

    sales_persons_list = []

    count = 0

    while count < 5:

        Name = input("What is the name of the sales person?: ")

        count += 1

        sales_persons_list.append(Name)

    print(sales_persons_list)


    return sales_persons_list


def inputSalesAmounts(sales_persons_list):

    sales_amounts_list = []

    index = 0

    while index < len(sales_persons_list):

        Sales =float(input("How much did " + str(sales_persons_list[index]) + " make in sales?: $"))

        index += 1

        sales_amounts_list.append(Sales)

    print(sales_amounts_list)    


    return sales_amounts_list 


**def displaySalesReport(sales_persons_list, sales_amounts_list):

    Total_Sales = sum(sales_amounts_list)

    print("%-15s %-15s" %("Salespeople","Sales Amount"))

    print("%-15s %-15s" %(sales_persons_list[0],sales_amounts_list[0]))

    print("%-15s %-15s" %(sales_persons_list[1],sales_amounts_list[1]))

    print("%-15s %-15s" %(sales_persons_list[2],sales_amounts_list[2]))

    print("%-15s %-15s" %(sales_persons_list[3],sales_amounts_list[3]))

    print("%-15s %-15s" %(sales_persons_list[4],sales_amounts_list[4]))

    print()

    print("%-15s %-15s" %("Totals", Total_Sales))**


main()

这将显示具有正确值的正确表,但是如您所见,它有点长且不干净。我该如何缩短?感谢您的帮助!


紫衣仙女
浏览 227回答 3
3回答

暮色呼如

此代码输出等于您的代码输出:)sales_persons_list = [input('What is the name of the sales person?: ') for i in range(5)]print(sales_persons_list)sales_amounts_list = [&nbsp; &nbsp; float(input('How much did {} make in sales?:'.format(sales_persons_list[i]))) for i in range(len(sales_persons_list))]print(sales_amounts_list)print("%-15s %-15s" %("Salespeople","Sales Amount"))for i in range(len(sales_amounts_list)):&nbsp; &nbsp; print("%-15s %-15s" %(sales_persons_list[i],sales_amounts_list[i]))print("%-15s %-15s" %("Totals", sum(sales_amounts_list)))将列表显示为表格代码:print("%-15s %-15s" %("Salespeople","Sales Amount"))for i in range(len(sales_amounts_list)):&nbsp; &nbsp; print("%-15s %-15s"%(sales_persons_list[i],sales_amounts_list[i]))print("%-15s %-15s" %("Totals", sum(sales_amounts_list)))输出Salespeople&nbsp; &nbsp; &nbsp;Sales Amounta&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1222.0aa&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1111.0aaa&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2222.0aaaaa&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3333.0aaaaaa&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1133.0Totals&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 9021.0

慕哥9229398

试试这个:def displaySalesReport(sales_persons_list, sales_amounts_list):&nbsp; &nbsp; Total_Sales = sum(sales_amounts_list)&nbsp; &nbsp; print("%-15s %-15s" %("Salespeople","Sales Amount"))&nbsp; &nbsp; for s_person, s_amount in zip(sales_persons_list, sales_amounts_list):&nbsp; &nbsp; &nbsp; &nbsp; print("%-15s %-15s" % (s_person, s_amount))&nbsp; &nbsp; print("%-15s %-15s" %("Totals", Total_Sales))这是代码的简化版,您也可以使用Python Pandas创建表,Pandas最适合表格形式的数据,但它提供的功能远远超出您的需要:import pandas as pddata = {'Salespeople': sales_persons_list, 'Sales Amount': sales_amounts_list}df = pd.DataFrame.from_dict(data)df.append({'Sales Amount': Total}, index="Total")print(df)我的代码是伪的,您必须对其进行修改以适合您的需求。

隔江千里

尝试使用许多流行的软件包之一来打印表格o / p。我的建议是使用tabulatefrom tabulate import tabulatesales_persons_list = list('ABCDE')sales_amounts_list = list('12345')print (tabulate(zip(sales_persons_list, sales_amounts_list), tablefmt="plain"))输出A&nbsp; 1B&nbsp; 2C&nbsp; 3D&nbsp; 4E&nbsp; 5
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python