Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join split words and punctuation with punctuation in the right place

So I tried using join() after splitting a string into words and punctuation but it joins the string with a space in between the word and punctuation.

b = ['Hello', ',', 'who', 'are', 'you', '?']
c = " ".join(b)

But that returns:
c = 'Hello , who are you ?'

and I want:
c = 'Hello, who are you?'

like image 688
user2270501 Avatar asked Apr 11 '13 13:04

user2270501


People also ask

How do you split a string in words and punctuation?

findall() method to split a string into words and punctuation, e.g. result = re. findall(r"[\w'\"]+|[,.!?] ", my_str) . The findall() method will split the string on whitespace characters and punctuation and will return a list of the matches.

How do you split text in words in Python?

Python String split() Method A string can be split into substrings using the split(param) method. This method is part of the string object. The parameter is optional, but you can split on a specific string or character. Given a sentence, the string can be split into words.

How do you split a sentence into a list in Python?

The simplest approach provided by Python to convert the given list of Sentences into words with separate indices is to use split() method. This method split a string into a list where each word is a list item.


1 Answers

You could join on the punctuation first:

def join_punctuation(seq, characters='.,;?!'):
    characters = set(characters)
    seq = iter(seq)
    current = next(seq)

    for nxt in seq:
        if nxt in characters:
            current += nxt
        else:
            yield current
            current = nxt

    yield current

c = ' '.join(join_punctuation(b))

The join_punctuation generator yields strings with any following punctuation already joined on:

>>> b = ['Hello', ',', 'who', 'are', 'you', '?']
>>> list(join_punctuation(b))
['Hello,', 'who', 'are', 'you?']
>>> ' '.join(join_punctuation(b))
'Hello, who are you?'
like image 107
Martijn Pieters Avatar answered Nov 10 '22 00:11

Martijn Pieters