关于AttributeError:'numpy.ndarray' object has no attribute 'index'
来源:互联网 发布:为什么金九银十 知乎 编辑:程序博客网 时间:2024/05/19 05:02
使用了一段时间的python,发现一个被忽略的ndarray和list的不同之处。
先上例子:
代码:
import numpy as np
array = [[0,0],[0,1],[0,2],[2,2],[3,3],[4,4]]
Indexs = [1,2,3]
print("first array:",array)
array = np.delete(array,Indexs,0)
print("second array:",array)
print(array.index([0,0]))
运行结果:
first array: [[0, 0], [0, 1], [0, 2], [2, 2], [3, 3], [4, 4]]
second array: [[0 0]
[3 3]
[4 4]]
Traceback (most recent call last):
File "C:\Users\admin\Desktop\meanshift\TEST.py", line 10, in <module>
print(array.index([0,0]))
AttributeError: 'numpy.ndarray' object has no attribute 'index'
运行结果可以看出,list数组和.ndarray的显示也有不同。
就会报错'numpy.ndarray' object has no attribute 'index',因为numpy.delete()删除过索引的对象后返回的是ndarray,而不是list,所以不能使用index查找元素的位置。
解决方法:ndarray.tolist()
import numpy as np
array = [[0,0],[0,1],[0,2],[2,2],[3,3],[4,4]]
Indexs = [1,2,3]
print("first array:",array)
array = np.delete(array,Indexs,0)
array = array.tolist()
print("second array:",array)
print(array.index([4,4]))
实验结果:
first array: [[0, 0], [0, 1], [0, 2], [2, 2], [3, 3], [4, 4]]
second array: [[0, 0], [3, 3], [4, 4]]
2
ok!!!
阅读全文
0 0
- 关于AttributeError:'numpy.ndarray' object has no attribute 'index'
- AttributeError: 'numpy.ndarray' object has no attribute 'index'
- AttributeError: 'numpy.ndarray' object has no attribute 'value_counts'
- 'numpy.ndarray' object has no attribute 'values'
- Python 保存数组至.mat文件,报错:AttributeError: 'numpy.ndarray' object has no attribute 'items'
- AttributeError: 'classmethod' object has no attribute '__module__'
- AttributeError: 'module' object has no attribute 'getpass'
- AttributeError: ‘NoneType’ object has no attribute ‘clone’
- AttributeError: 'module' object has no attribute 'Serial'
- AttributeError: 'NoneType' object has no attribute 'clone'
- AttributeError: 'tuple' object has no attribute 'Multi'
- AttributeError: 'Response' object has no attribute 'json'
- AttributeError: 'module' object has no attribute(转)
- AttributeError: 'Connection' object has no attribute 'connection_errors'
- AttributeError: 'module' object has no attribute 'randint'
- AttributeError: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'
- AttributeError: 'WebDriver' object has no attribute
- AttributeError: 'NoneType' object has no attribute
- 264. Ugly Number II
- 一个6年的Java程序员,针对分布式锁的案例分析和总结
- hbase初识---hbase shell操作以及对应java API开发
- 3.java.io.IOException
- Hbase的shell操作
- 关于AttributeError:'numpy.ndarray' object has no attribute 'index'
- 静态与非静态的方法或变量使用场合简要说明
- MathType如何输入任意长度的空格
- Minimum supported Gradle version is 3.3. Current version is 2.14.1.Please fix the project's Gradle s
- Building and Flashing AOSP for Device Nexus 5 hammerhead
- 逆序的三位数(c语言)
- java实现下载功能Controller层代码
- SpringCloud(第 054 篇)简单 Quartz-Cluster 微服务,采用注解配置 Quartz 分布式集群
- 错误1:malloc.c:2394