Nest all content into snippets
This commit is contained in:
28
snippets/python/s/sort-by-indexes.md
Normal file
28
snippets/python/s/sort-by-indexes.md
Normal file
@ -0,0 +1,28 @@
|
||||
---
|
||||
title: Sort list by indexes
|
||||
type: snippet
|
||||
language: python
|
||||
tags: [list]
|
||||
cover: little-white-flowers
|
||||
dateModified: 2020-11-02T19:28:35+02:00
|
||||
---
|
||||
|
||||
Sorts one list based on another list containing the desired indexes.
|
||||
|
||||
- Use `zip()` and `sorted()` to combine and sort the two lists, based on the values of `indexes`.
|
||||
- Use a list comprehension to get the first element of each pair from the result.
|
||||
- Use the `reverse` parameter in `sorted()` to sort the dictionary in reverse order, based on the third argument.
|
||||
|
||||
```py
|
||||
def sort_by_indexes(lst, indexes, reverse=False):
|
||||
return [val for (_, val) in sorted(zip(indexes, lst), key=lambda x: \
|
||||
x[0], reverse=reverse)]
|
||||
```
|
||||
|
||||
```py
|
||||
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']
|
||||
```
|
||||
Reference in New Issue
Block a user