msa
msa

Reputation: 725

how to remove word that is split into characters from list of strings

I have a list of sentences, where some of them contain only one word but it is split into characters. How can I either merge the characters to make it one word or drop the whole row?

list = ['What a rollercoaster', 'y i k e s', 'I love democracy']

Upvotes: 0

Views: 72

Answers (1)

Alexandre Marcq
Alexandre Marcq

Reputation: 365

I try to avoid writing regular expressions as much as I can, but from what you told me, this one could work :

import re

a = ['What a rollercoaster', 'y i k e s', 'I love democracy']

regex = re.compile(r'^(\w ){2,}.')
result = list(filter(regex.search, a)) 

This captures strings having at least two groups of character and space, followed by anything else. This is assuming you wouldn't have a sentence beginning with something like 'a a foo'.

Upvotes: 1

Related Questions