简体   繁体   中英

Flip rows in numpy array

I am currently working on a connect four game on python and encountered a problem while trying to print my board. I would like to display my board such that the row at the top appears at the bottom when the board is printed.

eg:

[1,0,0,0] [0,1,0,0] [0,0,1,0] becomes

[0,0,1,0] [0,1,0,0] [1,0,0,0] I have tried np.flip and np.flipud but that does not seem to work. This is my current code

def display_board(game): for i in range (game.rows): print(np.flip(game.mat[i,: :-1]))

Thank you!

You can just use array indexing. For example:

I = np.eye(3)

I is:

array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

and

I[::-1]

returns

array([[0., 0., 1.],
       [0., 1., 0.],
       [1., 0., 0.]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM