Check if all sides of a multidimensional numpy array are arrays of zeros

前端 未结 5 1500
青春惊慌失措
青春惊慌失措 2021-02-13 16:55

An n-dimensional array has 2n sides (a 1-dimensional array has 2 endpoints; a 2-dimensional array has 4 sides or edges; a 3-dimensional array has 6 2-dimensional faces; a 4-dime

5条回答
  •  旧巷少年郎
    2021-02-13 17:14

    Here's how you can do it:

    assert(all(np.all(np.take(x, index, axis=axis) == 0)
               for axis in range(x.ndim)
               for index in (0, -1)))
    

    np.take does the same thing as "fancy" indexing.

提交回复
热议问题