属性错误:'list'对象没有属性'split'

时间:2020-12-25 18:17:41

Using Python 2.7.3.1

使用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

我不明白我的编码有什么问题!我得到了这个错误:AttributeError:“list”对象没有属性“split”。

This is my code:

这是我的代码:

myList = ['hello']

myList.split()

2 个解决方案

#1


2  

You can simply do list(myList[0]) as below:

您可以简单地做列表(myList[0]),如下所示:

>>> myList = ['hello']
>>> myList=list(myList[0])
>>> myList
['h', 'e', 'l', 'l', 'o']

See documentation here

看到文档

#2


1  

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

更多关于列表理解的信息:http://www.secnetix.de/olli/python/list_overview .hawk。

Lists in python do not have a split method. split is a method of strings(str.split())

python中的列表没有分割方法。split是字符串的一种方法(string .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:

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

#1


2  

You can simply do list(myList[0]) as below:

您可以简单地做列表(myList[0]),如下所示:

>>> myList = ['hello']
>>> myList=list(myList[0])
>>> myList
['h', 'e', 'l', 'l', 'o']

See documentation here

看到文档

#2


1  

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

更多关于列表理解的信息:http://www.secnetix.de/olli/python/list_overview .hawk。

Lists in python do not have a split method. split is a method of strings(str.split())

python中的列表没有分割方法。split是字符串的一种方法(string .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:

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