python - Sorting columns in a table (list of lists) whilst preserving the correspondence of the rows -
for example:
list1 = ['c', 'b', 'a'] list2 = [3, 2, 1] list3 = ['11', '10', '01'] table = [list1, list2, list3]
i'd sort respect first column (list1), i'd final ordering still preserve lines (so after sorting i'd still have line 'b', 2, '10'). in example sort each list individually data can't that. what's pythonic approach?
one quick way use zip
:
>>> operator import itemgetter >>> transpose = zip(*table) >>> transpose.sort(key=itemgetter(0)) >>> table = zip(*transpose) >>> table [('a', 'b', 'c'), (1, 2, 3), ('01', '10', '11')]
Comments
Post a Comment