Reputation: 11
stl = ["one two three four five six"]
print(stl)
stl = stl.split()
print(stl)
This is in python 3. It says that:
list object has no attribute 'split'
I want it to give the reg string then when asked to print it again it will give it as a list.
Upvotes: 1
Views: 56
Reputation: 57
Try this one :
stl = ["one two three four five six"]
print(stl)
stl = stl[0].split()
print(stl)
Upvotes: 0
Reputation: 13
You've answered your own question (or, rather, the interpreter has answered it for you.) As the error message says, the list object has no attribute (i.e., method) named 'split'. Perhaps you meant to say
stl = "one two three four five six"
which creates a string variable and assigns it to 'stl', rather than
stl = ["one two three four five six"]
which creates a list variable containing 1 string element. The first form above would have produced the result that you were (presumably) looking for.
BTW, I'm not sure of the details (someone here can probably elaborate,) but you should use 'list_stack = list()' instead of 'list_stack = []' to create a new list. I believe the latter form will produce nasty surprises in some cases.
Upvotes: 1
Reputation: 10218
You are trying to split a list. split()
is a string function, but not a list function. To split the string in your list you can index the first element (a string) and call split
on that.
>>> stl[0].split()
['one', 'two', 'three', 'four', 'five', 'six']
Upvotes: 2