You can do this using a list comprehension.
def filter_possible_chars(corpus, last):
parts = [word.split(last) for word in corpus.split() if last in word]
return [w[1][0] for w in parts if w[1]]
print (filter_possible_chars('lazy languid line', 'la'))
print (filter_possible_chars('pitter patter batton', 'tt'))
print (filter_possible_chars('pitter pattor batt', 'tt'))
print (filter_possible_chars('pitter pattor batt', 'it'))
print (filter_possible_chars('pitter pattor batt', 'er'))
print (filter_possible_chars('pitter pattor batt', 'ox'))
You can combine the two lines into one long list comprehension as follows:
return [word.split(last)[1][0] for word in corpus.split() if last in word and word.split(last)[1]]
Let me explain the code:
parts = [word.split(last) for word in corpus.split() if last in word]
Here I am trying to split the corpus into individual words using
for word in corpus.split()
After that, I am checking if last
is in the individual word
If the substring last
exists, then i am splitting the word again with last
as the substring. This will give two sets of strings. The first part will be all characters before the substring in last
and the second part will be all characters after the substring in last
.
As an example, lazy
will get split as ['', 'zy']
for substring la
. Whereas pitter
will be split as ['pi', 'er']
for tt
Once you have this list, then you need to pick the first character from index 1.
for search la
:
lazy languid line
will result in [['', 'zy'], ['', 'nguid']]
for search `tt':
pitter patter batton
will result in [['pi', 'er'], ['pa', 'er'], ['ba', 'on']]
for search `tt':
pitter pattor batt
will result in [['pi', 'er'], ['pa', 'or'], ['ba', '']]
for search `er':
pitter pattor batt
will result in []
for search `ox':
pitter pattor batt
will result in []
This tells us that we can pick all the results provided the index 1 value has a string.
So the next list comprehension statement is:
return [w[1][0] for w in parts if w[1]]
Here, we are extracting each block from parts
and checking if index of 1 contains any string. If yes, then extract the 0th position and return it.
The output of the following statements are:
print (filter_possible_chars('lazy languid line', 'la'))
print (filter_possible_chars('pitter patter batton', 'tt'))
print (filter_possible_chars('pitter pattor batt', 'tt'))
print (filter_possible_chars('pitter pattor batt', 'it'))
print (filter_possible_chars('pitter pattor batt', 'er'))
print (filter_possible_chars('pitter pattor batt', 'ox'))
['z', 'n']
['e', 'e', 'o']
['e', 'o']
['t']
[]
[]