Remove words in relation to size and not in other list

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Remove words in relation to size and not in other list



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





Why is "je" not in the final output? Don't you want to delete words whose length == 3 and are not in not_shortword list?
– Johnny Mopp
Aug 6 at 14:30



"je"


length == 3


not in not_shortword





I messed up, I want to remove words that are less than 3...
– marin
Aug 6 at 14:37




3 Answers
3



In addition to missing split() on text, you've got your conditions a little mixed up.

Try:


split()


text


' '.join(
shortword for shortword in text.split(" ")
if len(shortword) >= 3 or shortword in not_shortword
)
# 'ne suis pas table'



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"



Make sure you are splitting the text into a list of words with .split():


.split()


remove_shortword = ' '.join(word for word in text.split() if len(word) >= 3 and word not in not_shortword)



Then join with a space instead of an empty string.





This code doesn't produce the desired result - the output here is just 'je une'.
– andrew_reece
Aug 6 at 14:33


'je une'





Returns je une
– Hearner
Aug 6 at 14:34


je une






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS