user3394677
user3394677

Reputation: 9

how to create a list from a string in python 3?

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

Upvotes: 0

Views: 130

Answers (4)

halex
halex

Reputation: 16403

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.

Upvotes: 1

TessellatingHeckler
TessellatingHeckler

Reputation: 28993

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)

Upvotes: 0

anon582847382
anon582847382

Reputation: 20361

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."] 

Upvotes: 2

Javier
Javier

Reputation: 1087

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."]

Upvotes: 0

Related Questions