I am given a string and a character position in that string. I want to get the n words before that position in a way that it does not include the last word if the character positon is in the middle of a word
text = 'the house is big the house is big the house is big'
char_nr = 19
list_of_words_before = text[:char_nr-1].split()
print(list_of_words_before) # we see that the string is splited in "the" I dont want hence the t in the list
nr_words = 3
if nr_words >len(list_of_words_before):
nr_words = len(list_of_words_before)
list_of_words_before[-nr_words:]
this gives:
['the', 'house', 'is', 'big', 't']
['is', 'big', 't']
but actually what I really want is ['house', 'is','big'] since t is just part of a word.
How would you make sure in the first place to divide by a space between words? Is any other solution?