0

So I am trying to turn a string with multiple separators into a list, but dictated by where the separators are:

ex: ("Hooray! Finally, we're done.", "!,") to be converted to: ['Hooray', ' Finally', " we're done."] based upon the separators given.

As you can see, the string is split into a list based on the separators. My closest attempt:

 for ch in separators:
    original = ' '.join(original.split(ch))
return(original.split())

when I do this I get the result:

['Hooray', 'Finally', "we're", 'done.']

but I need to have " we're done" as one element of the list, not separated.

I got a suggestion to use a string accumulator, but I don't see how it helps to solve the issue

Thanks

0

4 Answers 4

2

Just do this, using re.split:

>>> import re
>>> original = "Hooray! Finally, we're done."
>>> re.split('!|,', start)
['Hooray', ' Finally', " we're done."]

EDIT: Without regular expressions, you need a custom function such as:

def multisplit(s, delims):
    pos = 0
    for i, c in enumerate(s):
        if c in delims:
            yield s[pos:i]
            pos = i + 1
    yield list(s[pos:])

And then use it as so:

>>> original = "Hooray! Finally, we're done."
>>> multisplit(original, '!,')
['Hooray', ' Finally', " we're done."] 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response, but I don't think I am allowed to use the re.split method. We haven't learned it in class yet so I think I am stuck only able to go about it the long way using a for loop and some sort of accumulator.
@user3394677 See edits above. If I have been useful to you, kindly accept my answer.
1

You can use re.split with an appropriate expression:

>>> data=("Hooray! Finally, we're done.", "!,")
>>> re.split("[%s]" % re.escape(data[1]), data[0])
['Hooray', ' Finally', " we're done."]

Splits the first element of the tuple by every character in the tuples second element.

The regex is made out of the tuple's second string where all regex special charcters are properly escaped. [some chars] means that every character inside the Square Brackets will be a seperator.

1 Comment

Thanks for the response, but I don't think I am allowed to use the re.split method. We haven't learned it in class yet so I think I am stuck only able to go about it the long way using a for loop and some sort of accumulator.
0

A simple solutoin without regular expressions could be:

def split_on_separators(word, separators):
    word_list = [word]
    auxList = []
    for sep in separators:
        for w in word_list:
            auxList.extend(w.split(sep))
        word_list = auxList
        auxList = list()
    return word_list

example = "Hooray! Finally, we're done."
separators = '!,'

split_on_separators(example, separators)

Out[49]: ['Hooray', ' Finally', " we're done."]

Comments

0

Change the string so all the separators are the same, then split on that one:

def separate(words, separators):
    sep0 = separators[0]
    for sep in separators[1:]:
        words = words.replace(sep, sep0)

    return words.split(sep0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.