Formatting lists in table output columns (python 3)

I have data that is collected in a loop and stored in separate lists that contain only the same data types (for example, only rows, only floating), 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 considered these lists as “columns” of the table and wanted to print them as a formatted table, which 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 the data from the lists horizontally in the rows of the table, I looked online (and on this site) for some help regarding this problem, however I managed to find help to make it work in python 2.7 and not 3.5.1 what i use.
my question is:
how to get entries from the lists above to print them in a table as shown above.

Each index of an element from the above lists is associated (that is, record [0] from 4 lists is associated with the same element: bar, 0.05, 2.0, 40.0).

+4
source share
3 answers

Some interesting table draws with 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         |
+-----------+---------+-------+------------+

texttable pip install texttable.

+2

, , 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        
+1

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

I managed to find the solution I needed:

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

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

As soon as I got a new list of summaries, I started sorting and printing the table for the user (however, I will deal with 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='')

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 cdarke for the help :)

0
source

Source: https://habr.com/ru/post/1651748/


All Articles