Sorts one list based on another list containing the desired indexes.
zip()
and sorted()
to combine and sort the two lists, based on the values of indexes
.reverse
parameter in sorted()
to sort the dictionary in reverse order, based on the third argument.def sort_by_indexes(lst, indexes, reverse=False):
return [val for (_, val) in sorted(zip(indexes, lst), key=lambda x: \
x[0], reverse=reverse)]
a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk']
b = [3, 2, 6, 4, 1, 5]
sort_by_indexes(a, b) # ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges']
sort_by_indexes(a, b, True)
# ['oranges', 'milk', 'jam', 'eggs', 'bread', 'apples']
Python, List
Merges two or more lists into a list of lists, combining elements from each of the input lists based on their positions.
Python, List
Groups the elements of a list based on the given function and returns the count of elements in each group.
Python, List
Splits values into two groups, based on the result of the given filtering function.