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

Function that splits by delimiter, removes numerical only values and whitespaces or empty indices

I’m writing a function that splits by delimiter, removes numerical only values and whitespaces or empty indices.
However I can’t seem to get it to not print, or remove the empty indices that were split the the delimiter.

Say my sample is ABC//DEF/GH//I, I want it to split by "/" then remove the empty space that’s produced.
My output looks like this so far.

["ABC", "", "DEF", "GH", "", "I"]

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

What can I include to remove the "", bits?

def split_lines(lines, delimiter, remove = '[0-9]+$'):
  for line in lines:
    tokens = line.split(delimiter)
    tokens = [re.sub(remove, "", token) for token in tokens]
    print(tokens)

>Solution :

Try this one

lst = ["ABC", "", "DEF", "GH", "", "I"]


new_lst = list(filter(lambda e:e,lst))
print(new_lst)

OUTPUT

['ABC', 'DEF', 'GH', 'I']

If also want to remove ' ' a single space from the list then use this one

lst = ["ABC", " ", "DEF", "GH", " ", "I"]


new_lst = list(filter(lambda e:e.strip(),lst))
print(new_lst)

OUTPUT

['ABC', 'DEF', 'GH', 'I']
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