Created
October 24, 2016 07:21
-
-
Save fluency03/b8c8eb9c000b97d03f13e1bf9646ea9c to your computer and use it in GitHub Desktop.
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
| def compress_seq(seq, limit=5): | |
| ''' | |
| Compress a sequence to a short on by compress a long-continuously-apprearing | |
| sub-sequence into limited length. | |
| Args: | |
| seq: the sequence to be compressed. | |
| limit: the sub-sequence length limitation. | |
| Returns: | |
| A compressed sequence. | |
| ''' | |
| label_mark = '' | |
| count_mark = 0 | |
| new_seq = [] | |
| for idx, lb in enumerate(seq): | |
| if lb == label_mark: | |
| count_mark += 1 | |
| else: | |
| new_count = limit if count_mark > limit else count_mark | |
| new_seq += [label_mark] * new_count | |
| label_mark, count_mark = lb, 1 | |
| new_count = limit if count_mark > limit else count_mark | |
| new_seq += [label_mark] * new_count | |
| return new_seq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment