2

I'm trying to construct some presentable tables in python by combining data from two lists.

As an example, let's say I recorded the contestants in a race and I had the name, time and some other arbitrary measurement for each person:

headings = ['Name:','Time:','Awesomeness:']

info = [('Foo', 15.24242, 100), ('Bar', 421.333, 10), ('Pyth the Python', 3.333, 9000)]

I've applied some formatting by making the function neatomatic9000 to make the table's columns indented properly.

def neatomatic9000(something):
    print("{0: <20}".format(something), end = '')

for i in headings:
    neatomatic9000(i)
print('\n')
for j in info:
    for k in j:
        neatomatic9000(k)
    print('\n')

My table is printed like so:

Name:               Time:               Awesomeness:        

Foo                 15.24242            100                 

Bar                 421.333             10                  

Pyth the Python     3.333               9000 

Which looks okay, but I'm trying to make the table with the headings as a column on the left - essentially I'm trying to transpose it to look like this:

Name:               Foo               Bar            Pyth the Python

Time:               15.24242          421.333        3.333        

Awesomeness:        100               10             9000

EDIT: On another note, python infuriatingly seems to lack a check to see if something is a number regardless if its a float or an integer. I can't seem to incorporate a condition to check if an entry is a number, then round it to two decimal places with "{:.2f}".format(k)

user155876
  • 361
  • 2
  • 14

4 Answers4

1

Do you mean something like this?

for num, heading in enumerate(headings):
    neatomatic9000(heading)
    for j in info:
        neatomatic9000(j[num])
    print('\n')

Output:

Name:               Foo                 Bar                 Pyth the Python     

Time:               15.24242            421.333             3.333               

Awesomeness:        100                 10                  9000                

The enumerate function turns a list like [a, b, c] into something like [(0, a), (1, b), (2, c)]. We want one line per heading, so that's our outer loop. Then for the 0th heading, we print the 0th value for every contestant. For the next heading, we print the next value for every contestant, and so on.

Note that this is basically your loop, but inside out.

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • I need to start learning the enumerate function. Never even knew it existed as a built-in function in python. Thanks. Is there a way to include a check to make sure each non-string entry is rounded via {:.2f}".format(floatgoeshere) ? – user155876 Sep 02 '15 at 19:07
  • Yep! See @EMS's answer below. You could either check if something is a number (`isinstance(x, Number)`) or if it's not a string (`not isinstance(x, str)`). – Wander Nauta Sep 02 '15 at 19:09
1

With regards to determining whether a python object is a number (int, float, etc.), see here. Briefly, "from numbers import Number" ... "isinstance(obj, Number)"

Community
  • 1
  • 1
EMS
  • 1,033
  • 1
  • 9
  • 11
0

without looking at your code:

for left_heading in headings: # for every heading
    row = left_heading # start with the heading
    for info in infos: # from data
        row+= str(info[n])+'\t' # add data to heading
    n+=1 # move to next index
    row+='\n' # add spacing
    print row
taesu
  • 4,482
  • 4
  • 23
  • 41
0
headings = ['Name:','Time:','Awesomeness:']
info = [('Foo', 15.24242, 100), ('Bar', 421.333, 10), ('Pyth the Python', 3.333, 9000)]
info = [[info[v][i] for v in range(len(info))] for i, x in enumerate(info)]

def neatomatic9000(something):
    print("{0: <20}".format(something), end = '')

for i, j in enumerate(info):
    neatomatic9000(headings[i])
    for k in j:
        neatomatic9000(k)
    print('\n')
Cody Bouche
  • 945
  • 5
  • 10