Created
June 23, 2020 06:45
-
-
Save IvanaGyro/29ed1242f1c57b04df45bf3336869630 to your computer and use it in GitHub Desktop.
Sequence to lookup map -- a stupid function
This file contains hidden or 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
| from typing import Dict, List, Iterable, Hashable, Mapping, Sequence | |
| def seq2lookup( | |
| seqences: Iterable[Sequence], index: int, | |
| ) -> Dict[Hashable, Sequence]: | |
| """ | |
| Build the lookup table from a group of sequences. | |
| The lookup table is used to quickly find the target sequence with the | |
| corresponding item which is at the specific index of the target sequence. | |
| If more than one sequences where the value of the particular index is the | |
| same, then only the last sequence will be retained. | |
| Examples: | |
| ``` | |
| >>> seq2lookup( | |
| ... [ | |
| ... [1, 'dog', ['is', 'big']], | |
| ... (0, 1, '2'), | |
| ... [0, ('hashable',)], | |
| ... [0, None] | |
| ... ], | |
| ... 1, | |
| ... ) | |
| { | |
| 'dog': [1, 'dog', ['is', 'big']], | |
| 1: (0, 1, '2'), | |
| ('hashable',): [0, ('hashable',)], | |
| None: [0, None], | |
| } | |
| >>> seq2lookup([(1, 2, 3), (2, 3, 4), (5, 2, 7)], 1) | |
| {2: (5, 2, 7), 3: (2, 3, 4)} | |
| ``` | |
| Arguments: | |
| seqences: A group of sequences, each of which will be set as the value of the | |
| lookup table. | |
| index: The index where the key is picked from each sequence. | |
| """ | |
| return {seqence[index]: seqence for seqence in seqences} | |
| def seq2lookup_all( | |
| seq: Iterable[Sequence], index: int, | |
| ) -> Dict[Hashable, List[Sequence]]: | |
| pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment