问题
Using Python 2.7.3.1
I don't understand what the problem is with my coding! I get this error: AttributeError: 'list' object has no attribute 'split
This is my code:
myList = ['hello']
myList.split()
回答1:
You can simply do list(myList[0])
as below:
>>> myList = ['hello']
>>> myList=list(myList[0])
>>> myList
['h', 'e', 'l', 'l', 'o']
See documentation here
回答2:
To achieve what you are looking for:
myList = ['hello']
result = [c for c in myList[0]] # a list comprehension
>>> print result
['h', 'e', 'l', 'l', 'o']
More info on list comprehensions: http://www.secnetix.de/olli/Python/list_comprehensions.hawk
Lists in python do not have a split method. split is a method of strings(str.split()
)
Example:
>>> s = "Hello, please split me"
>>> print s.split()
['Hello,', 'please', 'split', 'me']
By default, split splits on whitespace.
Check out more info: http://www.tutorialspoint.com/python/string_split.htm:
来源:https://stackoverflow.com/questions/26942061/attributeerror-list-object-has-no-attribute-split