python3 二维数组的切片

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

这个在python2.7.10上运行无问题,但在python3.6.2上报错

TypeError: slice indices must be integers or None or have an __index__ method
阅读 3.2k
1 个回答

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进