-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from kouwenhovenlab/master
sync from lab repo.
- Loading branch information
Showing
7 changed files
with
304 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""misc.py | ||
Various utility functions. | ||
""" | ||
|
||
from typing import List, Tuple | ||
|
||
|
||
def reorder_indices(lst: List, target: List) -> Tuple[int]: | ||
""" | ||
Determine how to bring a list with unique entries to a different order. | ||
Supports only lists of strings. | ||
:param lst: input list | ||
:param target: list in the desired order | ||
:return: the indices that will reorder the input to obtain the target. | ||
:raises: ``ValueError`` for invalid inputs. | ||
""" | ||
if set([type(i) for i in lst]) != {str}: | ||
raise ValueError('Only lists of strings are supported') | ||
if len(set(lst)) < len(lst): | ||
raise ValueError('Input list elements are not unique.') | ||
if set(lst) != set(target) or len(lst) != len(target): | ||
raise ValueError('Contents of input and target do not match.') | ||
|
||
idxs = [] | ||
for elt in target: | ||
idxs.append(lst.index(elt)) | ||
|
||
return tuple(idxs) | ||
|
||
|
||
def reorder_indices_from_new_positions(lst: List[str], **pos: int) \ | ||
-> Tuple[int]: | ||
""" | ||
Determine how to bring a list with unique entries to a different order. | ||
:param lst: input list (of strings) | ||
:param pos: new positions in the format ``element = new_position``. | ||
non-specified elements will be adjusted automatically. | ||
:return: the indices that will reorder the input to obtain the target. | ||
:raises: ``ValueError`` for invalid inputs. | ||
""" | ||
if set([type(i) for i in lst]) != {str}: | ||
raise ValueError('Only lists of strings are supported') | ||
if len(set(lst)) < len(lst): | ||
raise ValueError('Input list elements are not unique.') | ||
|
||
target = lst.copy() | ||
for item, newidx in pos.items(): | ||
oldidx = target.index(item) | ||
del target[oldidx] | ||
target.insert(newidx, item) | ||
|
||
return reorder_indices(lst, target) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.