mirror of
https://github.com/rasbt/python_reference.git
synced 2024-11-23 20:11:13 +00:00
multiple list sorting
This commit is contained in:
parent
fbe3659fbf
commit
d2c0bbbef3
39
howtos_as_py_files/sorting_multiple_lists_by_col.py
Normal file
39
howtos_as_py_files/sorting_multiple_lists_by_col.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
# Sebastian Raschka 2014
|
||||
|
||||
"""
|
||||
You have 3 lists that you want to sort "relative" to each other,
|
||||
for example, picturing each list as a row in a 3x3 matrix: sort it by columns
|
||||
|
||||
########################
|
||||
If the input lists are
|
||||
########################
|
||||
|
||||
list1 = ['c','b','a']
|
||||
list2 = [6,5,4]
|
||||
list3 = ['some-val-associated-with-c','another_val-b','z_another_third_val-a']
|
||||
|
||||
########################
|
||||
the desired outcome is:
|
||||
########################
|
||||
|
||||
['a', 'b', 'c']
|
||||
[4, 5, 6]
|
||||
['z_another_third_val-a', 'another_val-b', 'some-val-associated-with-c']
|
||||
|
||||
########################
|
||||
and NOT:
|
||||
########################
|
||||
|
||||
['a', 'b', 'c']
|
||||
[4, 5, 6]
|
||||
['another_val-b', 'some-val-associated-with-c', 'z_another_third_val-a']
|
||||
|
||||
|
||||
"""
|
||||
|
||||
list1 = ['c','b','a']
|
||||
list2 = [6,5,4]
|
||||
list3 = ['some-val-associated-with-c','another_val-b','z_another_third_val-a']
|
||||
|
||||
|
||||
list1, list2, list3 = zip(*sorted(zip(list1, list2, list3)))
|
Loading…
Reference in New Issue
Block a user