Python 尝试拆分数字数组时发生AttributeError错误
我创建了一个数组Python 尝试拆分数字数组时发生AttributeError错误,python,arrays,Python,Arrays,我创建了一个数组arr1=[25,26]。当我尝试使用语句array1=arr1.split(',')基于逗号拆分数组时,我得到了错误: Traceback (most recent call last): File "<stdin>", line 1, in <module> array1 = arr1.split(',') AttributeError: 'list' object has no attribute 'split' 回溯(最近一次
arr1=[25,26]
。当我尝试使用语句array1=arr1.split(',')
基于逗号拆分数组时,我得到了错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
array1 = arr1.split(',')
AttributeError: 'list' object has no attribute 'split'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
array1=arr1.split(',')
AttributeError:“list”对象没有属性“split”
我哪里弄错了?
arr1=[25,26]
Firstarr1
不是数组,而是一个列表
对象
第二个split
不是list
属性的一部分,因此不能对list对象使用split
函数
您可以使用dir
内置函数查看所有list
属性
>>> dir([])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__',
'__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__set
attr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert'
, 'pop', 'remove', 'reverse', 'sort']
这就是为什么您会得到AttributeError
excecption,因为您正在对不属于list
属性的列表对象应用split
函数。split()不适用于列表项。它适用于字符串类型
在你的情况下,你不需要做分裂
很简单,您可以使用--
对于arr1中的项目:
打印项目
如果要检查阵列的长度。您可以执行-len(arra1)
python中没有数据类型数组。这是一张单子
要检查数据类型,只需使用-type(arr1)此拆分数组的预期结果是什么?
split
是-您期望的是什么[25,26]。拆分(',')
到底要做什么str.split
用于将字符串生成列表-您已经拥有的是一个列表。