Spaces:
Runtime error
Runtime error
from re import sub | |
def separate_string(string): | |
""" | |
This function returns a list of strings from a string. | |
Example: separate_string('1. swedish 2. nuclear 3. hello world 4. uha yhd ikv hahd vva 5. ') | |
returns ['swedish', 'nuclear', 'hello world', 'uha yhd ikv hahd vva', ''] | |
:param string: string to be separated | |
:return: list of string items | |
""" | |
list_string = string.split('.') | |
list_useable = [] | |
for list_part in list_string: | |
list_useable.append(list_part.split(' ', 1)) | |
final_list = [] | |
for li in list_useable[1:]: | |
final_list.append(li[1]) | |
# remove numeric characters and spaces | |
filter_numeric_regex = '[^a-z]' | |
final_final_list = [] | |
for li in final_list: | |
final_final_list.append(sub(filter_numeric_regex, ' ', li).strip()) | |
return final_final_list | |
def convert_to_tuple(string): | |
""" | |
This function converts a string to a tuple. | |
:param string: | |
:return: tuple of strings | |
""" | |
string = string.strip() | |
return tuple(string.strip('()').split(', ')) | |
if __name__ == '__main__': | |
s = ' (politics, government, negative, sweden)' | |
print(convert_to_tuple(s)) | |