AttributeError:“列表”对象没有属性“分割”

sp3*_*cro 5 python split attributeerror

使用 Python 2.7.3.1

我不明白我的编码有什么问题!我收到此错误:AttributeError: 'list' object has no attribute 'split

这是我的代码:

myList = ['hello']

myList.split()
Run Code Online (Sandbox Code Playgroud)

use*_*927 5

您可以简单地执行list(myList[0])以下操作:

>>> myList = ['hello']
>>> myList=list(myList[0])
>>> myList
['h', 'e', 'l', 'l', 'o']
Run Code Online (Sandbox Code Playgroud)

在这里查看文档


Tot*_*tem 2

为了实现您正在寻找的目标:

myList = ['hello']
result = [c for c in myList[0]] # a list comprehension

>>> print result
 ['h', 'e', 'l', 'l', 'o']
Run Code Online (Sandbox Code Playgroud)

有关列表推导式的更多信息:http://www.secnetix.de/olli/Python/list_compressives.hawk

python 中的列表没有 split 方法。str.split()split 是 strings( )的一种方法

例子:

>>> s = "Hello, please split me"
>>> print s.split()
['Hello,', 'please', 'split', 'me']
Run Code Online (Sandbox Code Playgroud)

默认情况下,分割按空格分割。

查看更多信息:http://www.tutorialspoint.com/python/string_split.htm