Skip to content
Advertisement

How can I find a word and print it along with his next word in Python

I have the following list:

JavaScript

From dataframe column I want to find texts that have the words of the list in order to generate a new column that can print these words along with the next word, for example, in a column cell there is the following text WOULD NOT PRIME CORRECTLY DURING VIRECTOMY., I want a new column that prints the following: WOULD NOT PRIME.

I have tried something like this

JavaScript

Advertisement

Answer

You can loop over every prefix in the list and check for the prefix using .find(). If it is found, you can change the prefix to the case of event and append the next word. Like this:

JavaScript

If you want to include the next two words, just change this line:

JavaScript

to a slice like this:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement