簡體   English   中英

將列表格式化為表輸出的列(python 3)

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

我有一個循環收集的數據,並存儲在只包含相同數據類型的單獨列表中(例如,只包含字符串,只有浮點數),如下所示:

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個列表的條目[0]與相同的項目相關聯;條形,0.05,2.0,40.0)。

一些有趣的表繪制與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         |
+-----------+---------+-------+------------+

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

這是一個小的實現,可以在基本的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        

訪問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的幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM