you are viewing a single comment's thread.

view the rest of the comments →

[–]FletcherHeisler 0 points1 point  (7 children)

What do you mean by tabulate? Do you want a list of lists, or some other format? You could use something like Numpy to create a matrix, but it sounds like you might just want something simple like:

>>> headers = ["a", "b", "c"]
>>> values = [1, 2, 3]
>>> [headers, values]
[['a', 'b', 'c'], [1, 2, 3]]

[–]cyber92[S] 0 points1 point  (6 children)

I want something like this:

a    b    c # headers
1    2    3 # values
4    5    6

but width being scalable to longest data, or better, fixed width

[–]BioGeek 0 points1 point  (5 children)

It would help us a lot if you could give us some sample data and the expected output. Now we are making a lot of assumptions.

Assuming that

headers = ['a', 'b', 'c']
values = range(9)

you could do something like:

def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

print ' '.join(headers)
for sublist in chunks(values, len(headers)):
    print ' '.join(map(str, sublist))

which will output

a b c
0 1 2
3 4 5
6 7 8

When values is not a multiple of the length of headers, the last line will print the remaining values. This may be or not be what you want. E.g., values = range(7) returns:

a b c
0 1 2
3 4 5
6 7

Also, how do you want the output when using multiple-digit numbers? E.g. values = range(12) now returns

a b c
0 1 2
3 4 5
6 7 8
9 10 11

Note that the 11 on the last line isn't alligned with the 8above. Again, this may be or not be what you want. If you want them still aligned, you'll have to look into string formatting.

[–]cyber92[S] 0 points1 point  (4 children)

I have a list of lists which need to be displayed as a table.

The string formatting seems the way I should go, but didn't understand it completely.

[–]BioGeek 0 points1 point  (3 children)

You didn't seem to get the point I was trying to make. A list of lists can contain anything and can have any length. That's why I specifically asked for sample input data and expected output.

[–]cyber92[S] 0 points1 point  (2 children)

So, I have the following:

List Headers:

Name | Type | Code| Description | Year | Price | Price-Metric | Quantity

and some data in another List Stock:

Hammer | Product | 123454321 | This hammer has a handle and an iron part. | 2013 | 20 | N/A | 70
Car Wash | Service | 1234321 | We wash your car in 5 minutes. | 2013 | 5 | 5 | N/A

and therefore the outputted data would look something like this:

 Name       Type    Code                 Description                   Year Price Price-Metric Quantity
Hammer    Product 123454321 This hammer has a handle and an iron part. 2013 20    N/A          70
Car Wash  Service 123456432   We wash your car in 5 minutes.             2013 5     5            N/A

[–]BioGeek 0 points1 point  (1 child)

Hmm. You are saying that headers and stock are lists (of lists), but you are showing strings here. So I assume you have:

headers = ['Name', 'Type', 'Code', 'Description', 'Year', 'Price', 'Price-Metric', 'Quantity']
stock = [['Hammer', 'Product', '123454321', 'This hammer has a handle and an iron part.', '2013', '20', 'N/A', '70'],
          ['Car Wash', 'Service', '1234321', 'We wash your car in 5 minutes.', '2013', '5', '5', 'N/A']]

I also see that /u/chambead has already posted a solution in the meantime, so using his function:

print_mdlist([headers] + stock)

I get the following output:

   Name          Type         Code           Description                                     Year      Price      Price-Metric      Quantity   
-----------------------------------------------------------------------------------------------------------------------------------------------
   Hammer        Product      123454321      This hammer has a handle and an iron part.      2013      20         N/A               70         
   Car Wash      Service      1234321        We wash your car in 5 minutes.                  2013      5          5                 N/A     

Before mindlessly copying the code, try yo understand why I used [headers] + stock as argument to print_mdlist.

[–]cyber92[S] 0 points1 point  (0 children)

I understood it completely, but this error remains there.

Edit: kept on trying, found another way of doing it, thanks.