I have a list which contains complete and incomplete sentences. The complete sentences are terminated with either .
, ?
, or !
. How do I loop through the elements in the list and join incomplete sentences?
For instance,
myList = ['My first','sentence is incomplete.','Hold on.','Is the second complete?','The third','might not be!','Thanks.','Really appreciate.']
myList = ['My first sentence is incomplete.','Hold on.','Is the second complete?','The third might not be!','Thanks','Really appreciate.']
Here is one brute-force method using collections.defaultdict
.
from collections import defaultdict
myList = ['My first','sentence is incomplete.','Hold on.','Is the second complete?',
'The third','might not be!','Thanks.','Really appreciate.']
d = defaultdict(list)
idx = 0
for j in myList:
d[idx].append(j)
if any(j.endswith(k) for k in ('.', '!', '?')):
idx += 1
sentences = [' '.join(v) for _, v in sorted(d.items())]
Result:
['My first sentence is incomplete.',
'Hold on.',
'Is the second complete?',
'The third might not be!',
'Thanks.',
'Really appreciate.']