Python AttributeError: 'list' 对象没有属性 'split'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26942061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:12:14  来源:igfitidea点击:

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

pythonsplitattributeerror

提问by sp3cro

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' object has no attribute 'split

This is my code:

这是我的代码:

myList = ['hello']

myList.split()

回答by Totem

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_comprehensions.hawk

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

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

Example:

例子:

>>> s = "Hello, please split me"
>>> print s.split()
['Hello,', 'please', 'split', 'me']

By default, split splits on whitespace.

默认情况下, split 在空白处拆分。

Check out more info: http://www.tutorialspoint.com/python/string_split.htm:

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

回答by user3885927

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

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

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

See documentationhere

请参阅此处的文档