You can just use a traditional for
loop like this:
phrases = [
'Title',
'page',
'Specification',
'High speed drilling ma-',
'chine is aimed at'
]
index_lst = []
merged_phrases = []
for index,phrase in enumerate(phrases):
if index not in index_lst:
if phrase[-1] == '-':
merged_phrases.append(phrase[:-1]+phrases[index+1])
index_lst.append(index+1)
else:
merged_phrases.append(phrase)
print(merged_phrases)
Output:
['Title', 'page', 'Specification', 'High speed drilling machine is aimed at']
Edit:
If consecutive words in your list have -
at the end, then you can use this code:
phrases = [
'Title',
'page',
'Specification',
'High speed drilling ma-',
'chine is aimed at-',
' test-',
' 123'
]
index_lst = []
merged_phrases = []
for index,phrase in enumerate(phrases):
curr_phrase = ''
if index not in index_lst:
for i,x in enumerate(phrases[index:]):
if x[-1] == '-':
curr_phrase += x[:-1]
index_lst.append(index+i)
else:
if curr_phrase != '':
merged_phrases.append(curr_phrase+x)
index_lst.append(index + i)
else:
merged_phrases.append(phrase)
break
print(merged_phrases)
Output:
['Title', 'page', 'Specification', 'High speed drilling machine is aimed at test 123']
CLICK HERE to find out more related problems solutions.