Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试拆分数字数组时发生AttributeError错误_Python_Arrays - Fatal编程技术网

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]
First
arr1
不是数组,而是一个
列表
对象

第二个
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
用于将字符串生成列表-您已经拥有的是一个列表。