Если у вас есть список строк и вы хотите проверить, образуют ли комбинации последовательных строк слово, вы можете перебирать строки и проверять возможные комбинации.Для этого вы можете использовать только встроенные средства Python:
LIMIT = 3 # max amount of strings to combine
def process_strings(strings, words):
ans = list()
stop = len(strings)
current = 0
# iterate over strings
while current < stop:
word = ''
counter = 0
# iterate over LIMIT strings starting from current string
while True:
# check boundary conditions
if counter >= LIMIT or current + counter >= stop:
current += 1
break
word += strings[current + counter]
# word found among words
if word in words:
current += 1 + counter
ans.append(word)
# print('found word: {}'.format(word))
break
# word not found
else:
counter += 1
return ans
words = {'victory', 'banana', 'python'}
strings = [
'vic', 'tory',
'mo', 'th', 'er',
'ban', 'ana',
'pyt', 'on',
'vict', 'ory',
'pyt', 'hon',
'vi', 'ct', 'or', 'y',
'ba', 'na', 'na']
words_found = process_strings(strings, words)
print('found words:\n{}'.format(words_found))
Выход:
found words:
['victory', 'banana', 'victory', 'python', 'banana']
РЕДАКТИРОВАТЬ
Модифицированная версия для 1) любое количество строк для объединения, 2) такие случаи, как words = {'victory', 'victor'}
, strings = ['vi', 'ct', 'or', 'y']
- будут найдены оба слова:
def process_strings(strings, words):
MAXLEN = max(map(len, words))
ans = list()
stop = len(strings)
current = 0
# iterate over strings
while current < stop:
word = ''
counter = 0
# iterate over some amount of strings starting from current string
while True:
# check boundary conditions
if len(word) > MAXLEN or current + counter >= stop:
current += 1
break
word += strings[current + counter]
# word found among words
if word in words:
ans.append(word)
# there is no case `word not found`, exit only by boundary condition (length of the combined substrings)
counter += 1
return ans