I think this is the simplest way for anyone else stumbling on this post given the late response:
>>> string = 'This is a string, with words!'
>>> string.split()
['This', 'is', 'a', 'string,', 'with', 'words!']
Answer from gilgamar on Stack OverflowI think this is the simplest way for anyone else stumbling on this post given the late response:
>>> string = 'This is a string, with words!'
>>> string.split()
['This', 'is', 'a', 'string,', 'with', 'words!']
Try this:
import re
mystr = 'This is a string, with words!'
wordList = re.sub("[^\w]", " ", mystr).split()
How it works:
From the docs :
re.sub(pattern, repl, string, count=0, flags=0)
Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged. repl can be a string or a function.
so in our case :
pattern is any non-alphanumeric character.
[\w] means any alphanumeric character and is equal to the character set [a-zA-Z0-9_]
a to z, A to Z , 0 to 9 and underscore.
so we match any non-alphanumeric character and replace it with a space .
and then we split() it which splits string by space and converts it to a list
so 'hello-world'
becomes 'hello world'
with re.sub
and then ['hello' , 'world']
after split()
let me know if any doubts come up.
python 3.x - List of strings to list of words - Stack Overflow
python - String to List of words - Stack Overflow
python - convert a word to a list of chars - Stack Overflow
python - How do I split a string into a list of words? - Stack Overflow
Videos
>>> s = "foobar"
>>> list(s)
['f', 'o', 'o', 'b', 'a', 'r']
In Python string is iterable. This means it supports special protocol.
>>> s = '123'
>>> i = iter(s)
>>> i
<iterator object at 0x00E82C50>
>>> i.next()
'1'
>>> i.next()
'2'
>>> i.next()
'3'
>>> i.next()
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
i.next()
StopIteration
list constructor may build list of any iterable. It relies on this special method next and gets letter by letter from string until it encounters StopIteration.
So, the easiest way to make a list of letters from string is to feed it to list constructor:
>>> list(s)
['1', '2', '3']
Given a string sentence, this stores each word in a list called words:
words = sentence.split()
To split the string text on any consecutive runs of whitespace:
words = text.split()
To split the string text on a custom delimiter such as ",":
words = text.split(",")
The words variable will be a list and contain the words from text split on the delimiter.
I'm working on a book I've downloaded from project Guttenberg and after doing some data cleaning I have ended up with a list of strings. Next I should split each string into words and collect all the words in a new list.
I have tried to use the split commando but somehow I end up with transforming each string into a list instead of a a string.
I'm sorry if I'm being vague. Please let me know if I should provide more infromation...
I have created the following function.
def splitter():
wordList = [s.split(" ") for s in book]
return wordList
book = splitter()
Somehow I end up with the following output when I type book[0] into the console:
['the',
'history',
'of',
'australian',
'exploration',
'from',
'1788',
'to',
'1888']
Instead of splitting the string and ending up with a list of strings I have created a list of lists.
What I want to end up with is:
In[] book[0]
Out[] the
and
in[] book[:9]
['the',
'history',
'of',
'australian',
'exploration',
'from',
'1788',
'to',
'1888']