1. 程式人生 > >python報錯:TypeError: slice indices must be integers or None or have an __index__ method

python報錯:TypeError: slice indices must be integers or None or have an __index__ method

宣告:本文為博主原創文章,不可轉載 https://blog.csdn.net/jjddss/article/details/73469104

在使用Python進行矩陣操作時,當內部含有除法時,會產生錯誤:

TypeError: slice indices must be integers or None or have an __index__ method

 

例如:

 

img=np.hstack((a[:,0:100/2],b[:,100/2,:])) 

由於除法/自動產生的型別是浮點型,因此出現上述錯誤,修正方法為,將/更改為//

程式碼為:

 

img=np.hstack((a[:,0:100//2],b[:,100//2,:]))