Last active
April 26, 2023 11:27
-
-
Save gartisk/c09736f05d02b922de585c6074055186 to your computer and use it in GitHub Desktop.
Python Snippets
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
def unpack_text(text, separator, expected_length = 0, begin = 0, default = None): | |
""" | |
Unpacks a text into a list of words separated by a separator, | |
with a given expected length, and a given beginning. | |
Returns a list of words, limited by the expected length and beginning, | |
with a default value if the length is shorter than expected. | |
Parameters: | |
text: string to unpack | |
separator: string to separate the text | |
expected_length: expected length of the list | |
begin: beginning of the list | |
default: default value to fill the list if the length is shorter than expected | |
""" | |
words = text.split(separator) | |
words_length = len(words) | |
expected_total = begin + expected_length or words_length | |
defaults = [default] * (expected_total - words_length) | |
list = [word.strip() for word in words] + defaults | |
limited_list = list[begin:expected_total] | |
return limited_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment