AttributeError: 'list' object has no attribute 'split'

£可爱£侵袭症+ 提交于 2019-12-22 06:31:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!