使用 Python 2.7.3.1
我不明白我的代码出了什么问题!我收到了这个错误:AttributeError:'list'对象没有'split'属性
这是我的代码:
myList = ['hello']
myList.split()
使用 Python 2.7.3.1
我不明白我的代码出了什么问题!我收到了这个错误:AttributeError:'list'对象没有'split'属性
这是我的代码:
myList = ['hello']
myList.split()
list(myList[0])
:>>> myList = ['hello']
>>> myList=list(myList[0])
>>> myList
['h', 'e', 'l', 'l', 'o']
请参考此处文档
。涉及IT技术相关内容。myList = ['hello']
result = [c for c in myList[0]] # a list comprehension
>>> print result
['h', 'e', 'l', 'l', 'o']
关于列表推导式的更多信息:http://www.secnetix.de/olli/Python/list_comprehensions.hawk
在 Python 中,列表没有 split 方法。split 是字符串的方法(str.split()
)。
示例:
>>> s = "Hello, please split me"
>>> print s.split()
['Hello,', 'please', 'split', 'me']
默认情况下,split函数会以空格为分隔符进行分割。
更多信息请查看:http://www.tutorialspoint.com/python/string_split.htm
.split()
方法。你想要达到什么目的? - senshin