Reputation: 953
I am trying to delete words that have three letters but that do not belong to a list. I tried to do this:
text = 'je ne suis pas une table'
not_shortword = {'n', 'pas', 'ne'}
remove_shortword = ''.join(shortword for shortword in text if len(shortword) > 4 and shortword not in not_shortword)
My output:
'je e suis pas ue table'
Good output:
ne suis pas table
Upvotes: 0
Views: 42
Reputation: 2729
The split() in your case breaks in an array of element by the space (one element of array = one word). Looping through it it the solution
text = 'je ne suis pas une table'
not_shortword = {'n', 'pas', 'ne'}
rep = ' '.join(shortword for shortword in text.split() if len(shortword ) >= 4 or shortword in not_shortword)
print(rep)
#returns : "ne suis pas table"
Upvotes: 1
Reputation: 21274
In addition to missing split()
on text
, you've got your conditions a little mixed up.
Try:
' '.join(
shortword for shortword in text.split(" ")
if len(shortword) >= 3 or shortword in not_shortword
)
# 'ne suis pas table'
Upvotes: 1