Regressor
Regressor

Reputation: 1973

How to filter list of strings based on word length using list comprehensions?

I am trying to filter out words in my list of strings that are equal to 1 characters or 2 characters. Here is my sample data -

l = ['new vaccine tech long term testing',
    'concerned past negative effects vaccines flu shot b',
    'standard initial screening tb never tb chest x ray']

I tried writing this logic but somehow, the output comes out as a list of words and not list of sentences

cleaner = [ ''.join(word) for each in l for word in each.split() if len(word) > 2 ]


cleaner
['new',
 'vaccine',
 'tech',
 'long',
 'term',
 'testing',
 'concerned',
 'past',
 'negative',
 'effects',
 'vaccines',
 'flu',
 'shot',
 'standard',
 'initial',
 'screening',
 'never',
 'chest',
 'ray']

How do I make this to output as below

output = ['new vaccine tech long term testing',
    'concerned past negative effects vaccines flu shot',
    'standard initial screening never chest ray']

Upvotes: 1

Views: 881

Answers (2)

mhhabib
mhhabib

Reputation: 3121

You can filter to get a certain size word of the string then join it to a list

l = ['new vaccine tech long term testing',
     'concerned past negative effects vaccines flu shot b',
     'standard initial screening tb never tb chest x ray']
res = [" ".join(filter(lambda x: len(x) > 2, eaach.split(' '))) for eaach in l]
print(res)

Upvotes: 2

Barmar
Barmar

Reputation: 781848

You need to use nested list comprehensions, not a single list comprehension. The outer one is for the sentences, the inner one is for the words.

And you need to join with a space, not an empty string, to put a space between the words.

output = [' '.join([word for word in sentence.split() if len(word) > 2]) for sentence in l]

Upvotes: 5

Related Questions