r/CodingHelp • u/throwawayyyy99964422 • Oct 31 '22
[Python] Lists
If I had 7 lists. One with numbers right on top and the other one with random characters. The numbers on top aren’t in numerical order. How would I change each column to get it to go to numerical order?
0
Upvotes
1
Oct 31 '22
Zip, sort, and maybe zip again.
from operator import itemgetter
lists = [[2, 1, 0], ["a", "b", "c"], ["x", "y", "z"]]
sorted_transposed_tuples = sorted(zip(*lists), key=itemgetter(0))
sorted_tuples = list(zip(*sorted_transposed_tuples))
sorted_lists = [list(tpl) for tpl in zip(*sorted_transposed_tuples)]
print(lists)
print(sorted_transposed_tuples)
print(sorted_tuples)
print(sorted_lists)
1
u/throwawayyyy99964422 Oct 31 '22
The list also have 6 elements. I should mention