【问题标题】:Formatting Lists into columns of a table output (python 3)将列表格式化为表格输出的列(python 3)
【发布时间】:2016-12-26 05:48:00
【问题描述】:

我有数据在循环中收集并存储在单独的列表中,这些列表仅包含相同的数据类型(例如,只有字符串,只有浮点数),如下所示:

names = ['bar', 'chocolate', 'chips']
weights = [0.05, 0.1, 0.25]
costs = [2.0, 5.0, 3.0]
unit_costs = [40.0, 50.0, 12.0]

我已将这些列表视为表格的“列”,并希望将它们打印为格式如下的表格:

Names     | Weights | Costs | Unit_Costs  
----------|---------|-------|------------
bar       | 0.05    | 2.0   | 40.0
chocolate | 0.1     | 5.0   | 50.0
chips     | 0.25    | 3.0   | 12.0

我只知道如何在表格行中水平打印列表中的数据,我已经在网上(和本网站)查看了有关此问题的一些帮助,但是我只设法找到了让它在 python 2.7 中工作的帮助而不是我正在使用的 3.5.1。
我的问题是:
如何从上述 4 个列表中获取条目以打印到如上所示的表格中。

上面列表中的每个项目索引都是相关联的(即 4 个列表中的 entry[0] 与同一项目相关联;bar, 0.05, 2.0, 40.0)。

【问题讨论】:

  • 看看字符串格式,以及内置的zip docs.python.org/3/library/functions.html#zip。 2.7的解决方案应该很容易转换为3.5,如果有疑问使用2to3.py就可以了。
  • 感谢您的快速回复,我会去网站看看:)
  • 嘿,我查看了链接并找到了一种方法让它为我工作。谢谢你的帮助:)
  • @JakeCannon 您可以发布解决方案作为您自己问题的答案。
  • 好的,我现在就这样做。抱歉,我是这个网站的新手。

标签: python list formatting columnsorting


【解决方案1】:

这是一个小实现,可以在基本 python 中执行您想要的操作(没有特殊模块)。


names = ['bar', 'chocolate', 'chips']
weights = [0.05, 0.1, 0.25]
costs = [2.0, 5.0, 3.0]
unit_costs = [40.0, 50.0, 12.0]


titles = ['names', 'weights', 'costs', 'unit_costs']
data = [titles] + list(zip(names, weights, costs, unit_costs))

for i, d in enumerate(data):
    line = '|'.join(str(x).ljust(12) for x in d)
    print(line)
    if i == 0:
        print('-' * len(line))

输出:


names       |weights     |costs       |unit_costs  
---------------------------------------------------
bar         |0.05        |2.0         |40.0        
chocolate   |0.1         |5.0         |50.0        
chips       |0.25        |3.0         |12.0        

【讨论】:

  • 感谢您,这种方法看起来非常有效,并且格式非常好。
  • 感谢分享。好消息是你不需要导入新库,缺点是如果值的长度是可变的,你需要调整传递给 ljust 的值来解决这个问题。
  • 如何让 ljust 匹配字符串的长度?
  • 假设您事先知道names 包含最长的输出,添加以下行:longest_string = max(map(len, names)) 并修改line = '|'.join(str(x).ljust(longest_string + 4) for x in d)。您可以使用它并根据需要对其进行格式化。
【解决方案2】:

使用texttable 绘制一些有趣的表格。

import texttable as tt
tab = tt.Texttable()
headings = ['Names','Weights','Costs','Unit_Costs']
tab.header(headings)
names = ['bar', 'chocolate', 'chips']
weights = [0.05, 0.1, 0.25]
costs = [2.0, 5.0, 3.0]
unit_costs = [40.0, 50.0, 12.0]

for row in zip(names,weights,costs,unit_costs):
    tab.add_row(row)

s = tab.draw()
print (s)

结果

+-----------+---------+-------+------------+
|   Names   | Weights | Costs | Unit_Costs |
+===========+=========+=======+============+
| bar       | 0.050   | 2     | 40         |
+-----------+---------+-------+------------+
| chocolate | 0.100   | 5     | 50         |
+-----------+---------+-------+------------+
| chips     | 0.250   | 3     | 12         |
+-----------+---------+-------+------------+

您可以使用此命令pip install texttable 安装texttable

【讨论】:

    【解决方案3】:

    访问docs.python.org/3/library/functions.html#zip后(链接由cdarke提供)

    我设法找到了我需要的解决方案:

    使用 zip 方法我创建了一个新的关联数据摘要列表:

    # sort into rows of associated data and convert to list
    rows = zip(names, weights, costs, unit_costs)
    summary = list(rows)
    

    获得新的摘要列表后,我开始对表格进行排序并将其打印给用户(不过,我稍后会处理格式):

    # Sort Alphabetically and print
    summary.sort()
    print()
    print("*** Results shown below (alphabetically) ***")
    print("Name\t\tWeight\tCost\tUnit Cost")
    for item in summary:
        print("")
        for data in item:
            print(data, "\t", end='')
    

    输出如下:

    *** Results shown below (alphabetically) ***
    Name        Weight  Cost    Unit Cost
    
    bar     0.05    2.0     40.0    
    chips   0.25    3.0     12.0    
    chocolate   0.1     5.0     50.0    
    

    感谢 cdarke 的帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      • 2020-11-12
      • 1970-01-01
      • 2015-01-20
      相关资源
      最近更新 更多