I have this program which should return (using linear searching) a list of all instances of single characters in 'corpus' that immediately follow 'last' (including duplicates). The characters should be in that same order as they appear in the corpus
Example:
filter_possible_chars('lazy languid line', 'la')
['z', 'n']
filter_possible_chars('pitter patter batton', 'tt')
['e', 'e', 'o']
filter_possible_chars('pitter pattor batt', 'tt')
['e', 'o']
But my program runs into a problem for the second example where after the third tt in the word batt, there is nothing after it so it obviously shouldnt put anything else in the list but I get the IndexError list index out of range?
This is the function:
def filter_possible_chars(corpus, last):
listo = []
last_list = []
final = []
for thing in corpus:
listo.append(thing)
for last_word in last:
last_list.append(last_word)
for index, letter in enumerate(listo):
if letter == last_list[0]:
if listo[index+1] == last_list[1]:
final.append(listo[index+2])
print(final)
question from:
https://stackoverflow.com/questions/65649589/linear-searching-with-two-letters-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…