Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to separate the strings inside a list of strings according to the appearance of certain characters inside the strings?

import re #to use matching regex inside list elements

separator_elements_list = ["'", "\"", "¿", "?", "¡", "!", "(", ")", "[", "]", "{", "}", ";", ",", ".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "&", "#", "$", "=", "+", "-", "*", "/", "~", " "]

list_verbs_in_this_input = ['llegamos  ', "allí')hacía", "/allá en el    centro. habrá"]  #input_list

evaluates_if_substring_is_a_verb_func(input_substring) # --> print here to check the result

I need to separate the elements of the list_verbs_in_this_input list if any of the separator symbols indicated in the separator_elements_list list appear, and also eliminate the empty strings or those that only contain whitespace.

This is how the list should look after filtering:

['llegamos', "allí", "hacía", "allá", "en", "el", "centro", "habrá"]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can split on the separators, then trim each part.

import re
pattern = "|".join(re.escape(sep) for sep in separator_elements_list)
res = [part.strip() for x in list_verbs_in_this_input 
           for part in re.split(pattern, x) if part]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading