Answers for "python sort 2 lists based on one"

2

sort a list by values of another one python

numbers = [1, 2, 3]
letters = ['c', 'a', 'b']

#The numbers array sorted by the letters order
sorted_list = [number for letter, number in sorted(zip(letters, numbers))]

print(sorted_list)
#prints: [2, 3, 1]
Posted by: Guest on January-04-2021
2

sort two lists by one python

list1 = [3,2,4,1,1]
list2 = ['three', 'two', 'four', 'one', 'one2']
list1, list2 = zip(*sorted(zip(list1, list2)))
Posted by: Guest on October-15-2020
1

python sort multiple lists based on sorting of single list

# Example usage:
list_of_lists = [[4,5,1,3,2], ['s','n','a','k','e'], ['p','l','a','n','e']]
list(map(list, list(zip(*sorted(zip(*list_of_lists), key=lambda sublist_to_sort_by: sublist_to_sort_by[0])))))
--> [[1, 2, 3, 4, 5], ['a', 'e', 'k', 's', 'n'], ['a', 'e', 'n', 'p', 'l']]
# This is involved to explain. Best way to understand it is to test
#	the components of the function from the inside out.
Posted by: Guest on October-04-2020
0

sort two lists that refence each other

>>> list1 = [3,2,4,1, 1]
>>> list2 = ['three', 'two', 'four', 'one', 'one2']
>>> list1, list2 = zip(*sorted(zip(list1, list2)))
>>> list1
(1, 1, 2, 3, 4)
>>> list2 
('one', 'one2', 'two', 'three', 'four')
Posted by: Guest on November-30-2020
0

sort list with respect to another list

list1 = [[1,1],[1,2],[1],[2,2]]
list2 = [2, 3, 1, 4]

zipped_lists = zip(list1, list2)
sorted_pairs = sorted(zipped_lists)

tuples = zip(*sorted_pairs)
c,v = [ list(tuple) for tuple in  tuples]
print(c)
Posted by: Guest on August-18-2020

Code answers related to "python sort 2 lists based on one"

Python Answers by Framework

Browse Popular Code Answers by Language