将列表格式化为表输出的列(python 3)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39032720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:47:33  来源:igfitidea点击:

Formatting Lists into columns of a table output (python 3)

pythonlistformattingcolumnsorting

提问by Jake Cannon

I have data that is collected in a loop and stored under separate lists that hold only the same datatypes (e.g. only strings, only floats) as shown below:

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

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]

I have treated these lists as "columns" of a table and wish to print them out as a formatted table that should look something like this:

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

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

I only know how to print out data from lists horizontally across table rows, I have looked online (and on this site) for some help regarding this issue, however I only managed to find help for getting it to work in python 2.7 and not 3.5.1 which is what I am using.
my question is:
how do I get entries from the above 4 lists to print out into a table as shown above.

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

Each item index from the lists above is associated (i.e. entry[0] from the 4 lists is associated with the same item; bar, 0.05, 2.0, 40.0).

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

回答by Israel Unterman

Here is a small implementation that does what you want in basic python (no special modules).

这是一个小型实现,可以在基本 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))

Output:

输出:


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        

回答by Rahul K P

Some interesting table draw with texttable.

一些有趣的表格绘制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)

Result

结果

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

You can install texttablewith using this command pip install texttable.

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

回答by Jake Cannon

After visiting docs.python.org/3/library/functions.html#zip (link provided by cdarke)

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

I managed to find the solution I needed:

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

using the zip method I created a new summary list of the associated data:

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

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

Once I had the new summary list, I proceeded to sort and print out the table to the user (however, I will deal with the formatting later):

有了新的摘要列表后,我开始对表格进行排序并将其打印给用户(但是,稍后我将处理格式):

# 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='')

output is as follows:

输出如下:

*** 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    

Thanks to cdarke for the help :)

感谢 cdarke 的帮助:)