Python 根据列表计算并将输出打印为表格
我是python新手 我有以下清单: 硬件=[“主板”、“cpu”、“gpu”] 金额=[5,8,4,6] 成本=[210.0250.5360.8] 我想打印一个输出,你可以在下面链接中提供的txt文件中看到 我的尝试如下:Python 根据列表计算并将输出打印为表格,python,list,printing,sum,Python,List,Printing,Sum,我是python新手 我有以下清单: 硬件=[“主板”、“cpu”、“gpu”] 金额=[5,8,4,6] 成本=[210.0250.5360.8] 我想打印一个输出,你可以在下面链接中提供的txt文件中看到 我的尝试如下: hardware = ["motherboard", "cpu", "gpu"] amount = [5, 8, 4, 6] cost = [210.0, 250.5, 360.8] product_cost = [a*b for a,b in zip(amount,
hardware = ["motherboard", "cpu", "gpu"]
amount = [5, 8, 4, 6]
cost = [210.0, 250.5, 360.8]
product_cost = [a*b for a,b in zip(amount, cost)]
total = sum(product_cost)
titles = ['Hardware', 'Amount', 'Cost per item', 'Total cost per hardware']
data = [titles] + list(zip(hardware, amount, cost, product_cost))
for i, d in enumerate(data):
line = ' '.join(str(x).ljust(12) for x in d)
print(line)
if i == 0:
print(' ' * len(line))
print('\n' "Total cost: " + str(total))
但我得到的输出并不是您在txt文件中看到的理想输出
我附上txt文件。以下是txt的链接:
你能帮我得到想要的结果吗。这应该符合你想要的结果。您可以根据自己的需要调整表中的间距
hardware = ["motherboard", "cpu", "gpu"]
amount = [5, 8, 4, 6]
cost = [210.0, 250.5, 360.8]
product_cost = [a*b for a,b in zip(amount, cost)]
total = sum(product_cost)
titles = ['Hardware', 'Amount', 'Cost per item', 'Total cost per hardware']
data = [titles] + list(zip(hardware, amount, cost, product_cost))
for i in range(len(data)):
if i == 0:
print('{:<15s}{:>10s}{:>20s}{:>30s}'.format(data[i][0],data[i][1],data[i][2],data[i][3]))
print()
else:
print('{:<15s}{:>10d}{:>20.2f}{:>30.2f}'.format(data[i][0],data[i][1],data[i][2],data[i][3]))
print('\n' "Total cost: %.2f" % (total))
hardware=[“主板”、“cpu”、“gpu”]
金额=[5,8,4,6]
成本=[210.0250.5360.8]
产品成本=[a*b为a,b为邮政编码(金额、成本)]
总计=总和(产品成本)
标题=[‘硬件’、‘金额’、‘每项成本’、‘每硬件总成本’]
数据=[标题]+列表(zip(硬件、金额、成本、产品成本))
对于范围内的i(len(数据)):
如果i==0:
打印({:10s}{:20s}{:30s})。格式(数据[i][0]、数据[i][1]、数据[i][2]、数据[i][3]))
打印()
其他:
打印({:10d}{:>20.2f}{:>30.2f})。格式(数据[i][0],数据[i][1],数据[i][2],数据[i][3]))
打印(“\n”总成本:%.2f”%(总成本))
首先,您必须将行转换为列,并计算每列的最大长度
rows = [titles] + list(zip(hardware, amount, cost, product_cost))
columns = list(zip(*rows))
lengths = [max([len(str(x)) for x in col]) for col in columns]
接下来,您必须将行中的每个元素单独显示,因为第一列需要ljust
,其他列需要rjust
——并且所有这些列都需要不同于长度的值
因为第一列中的文本比标题长,所以我在第二列中使用了额外的elif
。让它更具普遍性需要更多的工作
hardware = ["motherboard", "cpu", "gpu"]
amount = [5, 8, 4, 6]
cost = [210.0, 250.5, 360.8]
product_cost = [a*b for a,b in zip(amount, cost)]
total = sum(product_cost)
titles = ['Hardware', 'Amount', 'Cost per item', 'Total cost per hardware']
rows = [titles] + list(zip(hardware, amount, cost, product_cost))
columns = list(zip(*rows))
lengths = [max([len(str(x)) for x in col]) for col in columns]
#print(lengths)
for y, row in enumerate(rows):
for x, item in enumerate(row):
l = lengths[x]
if x == 0:
print(str(item).ljust(l), end='')
elif x == 1:
print(str(item).rjust(l+2), end='')
else:
print(str(item).rjust(l+5), end='')
print()
if y == 0:
print()
print('\nTotal cost: {:.2f}'.format(total))
结果
Hardware Amount Cost per item Total cost per hardware
motherboard 5 210.0 1050.0
cpu 8 250.5 2004.0
gpu 4 360.8 1443.2
Total cost: 4497.20
编辑:与模块类似
结果:
Hardware Amount Cost per item Total cost per hardware
----------- -------- --------------- -------------------------
motherboard 5 210.0 1050.0
cpu 8 250.5 2004.0
gpu 4 360.8 1443.2
Total cost: 4497.20
由于您的问题在于格式,所以可以使用任何制作整洁表格的模块。但仍需等待,其他人可以提供帮助。列具有不同的宽度,因此首先您应该为列中的所有元素获取len()
,并获取max()
length。并仅在第一列中使用ljust()
。其他列需要rjust()
。或者找到类似的模块
Hardware Amount Cost per item Total cost per hardware
----------- -------- --------------- -------------------------
motherboard 5 210.0 1050.0
cpu 8 250.5 2004.0
gpu 4 360.8 1443.2
Total cost: 4497.20