How to make a checkerboard in numpy?

后端 未结 24 1323
小鲜肉
小鲜肉 2020-11-30 07:52

I\'m using numpy to initialize a pixel array to a gray checkerboard (the classic representation for \"no pixels\", or transparent). It seems like there ought to be a whizzy

24条回答
  •  日久生厌
    2020-11-30 08:04

    n = int(input())
    import numpy as np
    m=int(n/2)
    a=np.array(([0,1]*m+[1,0]*m)*m).reshape((n,n))
    
    print (a)
    

    So if input is n = 4 then output would be like:

    [[0 1 0 1]
     [1 0 1 0]
     [0 1 0 1]
     [1 0 1 0]]
    

提交回复
热议问题