将Python 3.6.5列表转换为表

时间:2018-07-25 16:14:08

标签: python python-3.x

我目前正在从事一个项目,我需要将列表转换为表格的帮助。我已经想出了一种方法,但是必须有一种更短,更清洁的方法来做,而我很难弄清楚。我的编码如下(我需要帮助的部分在**的底部)

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()

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

3 个答案:

答案 0 :(得分:0)

尝试一下:

def displaySalesReport(sales_persons_list, sales_amounts_list):
    Total_Sales = sum(sales_amounts_list)
    print("%-15s %-15s" %("Salespeople","Sales Amount"))
    for s_person, s_amount in zip(sales_persons_list, sales_amounts_list):
        print("%-15s %-15s" % (s_person, s_amount))
    print("%-15s %-15s" %("Totals", Total_Sales))

这是代码的简化版本,您也可以使用Python Pandas创建表,Pandas最适合表格形式的数据,但它提供的功能远远超出您的需要:

import pandas as pd

data = {'Salespeople': sales_persons_list, 'Sales Amount': sales_amounts_list}
df = pd.DataFrame.from_dict(data)
df.append({'Sales Amount': Total}, index="Total")
print(df)

我的代码是伪代码,您必须对其进行修改以适合您的需求。

答案 1 :(得分:0)

尝试使用许多流行的软件包之一来打印表格o / p。我的建议是使用tabulate

from tabulate import tabulate
sales_persons_list = list('ABCDE')
sales_amounts_list = list('12345')
print (tabulate(zip(sales_persons_list, sales_amounts_list), tablefmt="plain"))

输出

A  1
B  2
C  3
D  4
E  5

答案 2 :(得分:0)

此代码输出等于您的代码输出:)

sales_persons_list = [input('What is the name of the sales person?: ') for i in range(5)]
print(sales_persons_list)
sales_amounts_list = [
    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)):
    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)):
    print("%-15s %-15s"%(sales_persons_list[i],sales_amounts_list[i]))
print("%-15s %-15s" %("Totals", sum(sales_amounts_list)))

输出

Salespeople     Sales Amount
a               1222.0
aa              1111.0
aaa             2222.0
aaaaa           3333.0
aaaaaa          1133.0
Totals          9021.0