Use the list constructor:
>>> list("foobar")
['f', 'o', 'o', 'b', 'a', 'r']
list builds a new list using items obtained by iterating over the input iterable. A string is an iterable -- iterating over it yields a single character at each iteration step.
Use the list constructor:
>>> list("foobar")
['f', 'o', 'o', 'b', 'a', 'r']
list builds a new list using items obtained by iterating over the input iterable. A string is an iterable -- iterating over it yields a single character at each iteration step.
You take the string and pass it to list()
s = "mystring"
l = list(s)
print l
Split string to list without split method
Splitting a list in sublists by values
Split a list into sublists using an array of indexes
Splitting an input sentence into a list
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']