关于AttributeError:'numpy.ndarray' object has no attribute 'index'
2017-12-17 15:16
2759 查看
使用了一段时间的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!!!
先上例子:
代码:
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!!!
相关文章推荐
- AttributeError: 'numpy.ndarray' object has no attribute 'index'
- AttributeError: 'numpy.ndarray' object has no attribute 'value_counts'
- Python 保存数组至.mat文件,报错:AttributeError: 'numpy.ndarray' object has no attribute 'items'
- 'numpy.ndarray' object has no attribute 'values'
- AttributeError: 'module' object has no attribute 'randint'
- AttributeError: '_NamespacePath' object has no attribute 'sort'
- AttributeError: 'module' object has no attribute 'text_format'
- AttributeError:'module' object has no attribute 'classify'
- 解决方法——AttributeError: 'module' object has no attribute 'VideoWriter_fourcc'
- AttributeError: 'module' object has no attribute 'AF_INET'
- AttributeError: 'Bunch' object has no attribute 'data'
- AttributeError: 'WebDriver' object has no attribute
- 【TensorFlow问题】AttributeError:'module' object has no attribute 'mul'
- AttributeError: 'module' object has no attribute 'text_format'
- AttributeError: 'NoneType' object has no attribute 'sc' 解决方法(二)
- AttributeError: 'NoneType' object has no attribute 'astyp
- AttributeError: 'module' object has no attribute 'XXX'
- AttributeError: 'dict' object has no attribute 'iteritems'
- AttributeError: 'module' object has no attribute 'text_format'
- 执行《机器学习实战》15章代码15-3出现AttributeError: 'MRmean' object has no attribute 'mr'