鍍金池/ 問(wèn)答/數(shù)據(jù)分析&挖掘  Python/ python3 二維數(shù)組的切片

python3 二維數(shù)組的切片

dims = np.array([[1, 2, 3, 4],[5, 6, 7, 8]])
c = 2
result = dims[c/2::1, c/2::1]

這個(gè)在python2.7.10上運(yùn)行無(wú)問(wèn)題,但在python3.6.2上報(bào)錯(cuò)

TypeError: slice indices must be integers or None or have an __index__ method
回答
編輯回答
負(fù)我心

result = dims[c//2::1, c//2::1]
python3 中 //是整除
例如 2/1 #2.0, 2//1 #2

2018年9月23日 11:55