Search code examples
pythonlistconcatenationstring-concatenation

Concatenating incomplete setences from lists in Python


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,

Input:

myList = ['My first','sentence is incomplete.','Hold on.','Is the second complete?','The third','might not be!','Thanks.','Really appreciate.']

Desired output:

myList = ['My first sentence is incomplete.','Hold on.','Is the second complete?','The third might not be!','Thanks','Really appreciate.']

Solution

  • 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.']