Here is alternative with rank order of headers for reading order of the rows:
from __future__ import print_function
table = [
['c', 'b', 'd', 'aa', 'ba', 'aab'],
list(range(2,2+6)),
list(range(6,6+6))]
ranks = sorted(zip(table[0], range(len(table[0]))))
ranks= dict((r, ind) for r, (header, ind) in enumerate(ranks))
m = max(len(str(item)) for row in table for item in row)
for row in table:
for key in range(len(row)):
print (' %*s' % (m, row[ranks[key]]), end=',')
print()
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
You can save the rank order in old table by slice assignment from generator:
for rowno,row in enumerate(table):
table[rowno][:] = (row[ranks[ind]] for ind in range(len(ranks)))
print(table)
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852