Advertisement
here2share

# sort_by_indexes.py

Jul 20th, 2022
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. # sort_by_indexes.py
  2.  
  3. def sort_by_indexes(lst, indexes, reverse=False):
  4.   return [val for (_, val) in sorted(zip(indexes, lst), key=lambda x: \
  5.           x[0], reverse=reverse)]
  6. a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk']
  7. b = [3, 2, 6, 4, 1, 5]
  8. sort_by_indexes(a, b) # ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges']
  9. sort_by_indexes(a, b, True)
  10. # ['oranges', 'milk', 'jam', 'eggs', 'bread', 'apples']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement