def sort_columns(table, **kwords):
	return list(zip(*sorted(zip(*table), **kwords)))

table = [
	['c', 'b', 'd', 'aa', 'ba', 'aab'],
	list(range(2,2+6)),
	list(range(6,6+6))]

for row in sort_columns(table): 
    print(row)

Specialties:
IT/Science/Contracts/Religious translation/interpreting FIN-ENG-FIN
Python programming

1
Contributor
2
Replies
5
Views
5 Years
Discussion Span
Last Post by pyTony
0

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()
0

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)

Edited by pyTony: n/a

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.