Skip to content

Instantly share code, notes, and snippets.

@malcolmgreaves
Created January 21, 2022 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcolmgreaves/d996ab53242e9aacc10a797b25070a08 to your computer and use it in GitHub Desktop.
Save malcolmgreaves/d996ab53242e9aacc10a797b25070a08 to your computer and use it in GitHub Desktop.
Re-sort any list given a list of new index positions.
from typing import TypeVar
from toolz import first, second
T = TypeVar('T')
def align(new_positions: List[int], items: List[T]) -> List[T]:
"""Re-sort any list given a list of new index positions.
"""
return list(map(second, sorted(list(zip(new_positions, items)), key=first)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment