python - 从列表中计算并将输出打印为表格
问题描述
我是蟒蛇的新手。
我有以下列表:
硬件 = [“主板”、“cpu”、“gpu”]
数量 = [5, 8, 4, 6]
成本 = [210.0, 250.5, 360.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, 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的链接:
https://drive.google.com/open?id=1vANzMk9z2cxTWJRlwH3AkudN_jlG3iah
你能帮我得到想要的结果吗?
解决方案
首先,您必须将行转换为列并计算每列的最大长度
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
- 并且它们都需要不同的值lenghts
因为第一列中的文本比标题长,所以我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 = ["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 = list(zip(hardware, amount, cost, product_cost))
import tabulate
print(tabulate.tabulate(rows, headers=titles, floatfmt=".1f"))
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
推荐阅读
- c# - 在接口 c# 8 中使用静态、内部和受保护的访问修饰符
- python - 在 Docker 中运行 Flask 应用程序会出现“无法建立连接”错误?
- angular - 检测输入数组对象的变化
- core-data - SwiftUI 和 Core Data:基于用户输入获取
- visual-studio - SSIS 永久锁定
- algorithm - 运行时计算的规则总和不适用于一种情况......我做得对吗?
- java - org.gradle.api.UncheckedIOException:java.io.IOException:无法运行程序:CreateProcess error=206,文件名或扩展名太长
- css - CSS Flexbox:3个弹性项目,2个列(宽度等于内容)和1个增长到宽度的项目
- spring - 更改 Spring Cloud Dataflow Stream 部件的服务名称
- php - 如何将图像保存在 CodeIgniter 根文件夹和数据库中?