美文网首页python
Python错误总结:

Python错误总结:

作者: Setsuna_F_Seiei | 来源:发表于2019-09-25 18:51 被阅读0次

1. OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cp

解决办法:删掉cv2模块包,再从cmd中用pip(或conda)删除opencv-python模块,再重新安装。

2. windows查看开关机时间。

查看windows10开关机时间
3.'int' object is not callable

类似的有:'str' object is not callable,'list' object is not callable...
报错原因:主要是因为定义变量时与定义的函数或内置函数重名导致的,在调用函数时报此类错误。
如:

len = 123   #与内置函数len()重名。
a = 'aaa'
len(a)

报错:'int' object is not callable
总结:因此,在定义函数或变量时,千万不要与已定义的函数或内置函数重名。

4.list indices must be integers or slices, not tuple

报错原因:list对象不能进行二维切片,只能进行一维切片,而numpy.ndarray对象则能进行多维切片。
如:

b = list([['a', 'b', 'c', 'd'],
   ['e', 'f', 'g', 'h'],
   ['i', 'j', 'k', 'l'],
   ['m', 'n', 'o', 'p'],
   ['q', 'r', 's', 't'],
   ['u', 'v', 'w', 'x']])
print(b[1:5, 1:3])

报错:list indices must be integers or slices, not tuple
然而:

c = np.array([['a', 'b', 'c', 'd'],
   ['e', 'f', 'g', 'h'],
   ['i', 'j', 'k', 'l'],
   ['m', 'n', 'o', 'p'],
   ['q', 'r', 's', 't'],
   ['u', 'v', 'w', 'x']])
print(c[1:5, 1:3])

结果:

[['f' 'g']
 ['j' 'k']
 ['n' 'o']
 ['r' 's']]

总结:因为list不能进行多维切片,则又想要数组截取时,可以采用以下解决方法:

1.转换成numpy.ndarray对象。
d = np.array(b)
print(d[1:5, 1:3])

结果:

[['f' 'g']
 ['j' 'k']
 ['n' 'o']
 ['r' 's']]

再转回来:
注意:list()只能把0轴的numpy.ndarray对象转化为list,所以需要循环把里面的numpy.ndarray对象也转化为list。

g = []
for i in f:
    g.append(list(i))
print(g)

结果:

[['f', 'g'], ['j', 'k'], ['n', 'o'], ['r', 's']]
2.通过循环借取。
a=[[1,2,3], [4,5,6]]
b = [i[0] for i in a]
print(b)

结果:

[1, 4]
3.通过在类中实现getitem特殊方法。

具体链接:https://www.jb51.net/article/103555.htm

相关文章

网友评论

    本文标题:Python错误总结:

    本文链接:https://www.haomeiwen.com/subject/ukavuctx.html