【问题标题】:How to rearrange matrix elements vertically on pythonHow to rearrange matrix elements vertically on python
【发布时间】:2022-12-02 01:33:25
【问题描述】:

I'm trying to build a basic game-like program where I need to rearrange a given matrix but vertically. In this case, I only have 0s and 1s. 0 being lighter objects and 1 being heavier. When the function runs, all the 1s should fall down vertically and the zeros go up vertically as well. It needs to have the exact number of 0s and 1s as the original matrix. Example: -If I give the following matrix:

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

It should rearrange it to:

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

Any help or suggestions will be highly appreciated.

【问题讨论】:

  • Your examples are not valid python. How are you storing these matrices? As numpy arrays? As lists of rows which are themselves list? Some other way?

标签: python matrix


【解决方案1】:

Consider using numpy for your matrices. You can then use np.sort to do what you want:

np.sort(matrix, axis=0)

【讨论】:

    猜你喜欢
    • 2022-12-15
    • 2022-12-01
    • 2022-12-27
    • 2022-12-02
    • 2022-12-01
    • 2022-12-01
    • 2022-11-20
    • 2022-12-01
    • 2022-12-27
    相关资源
    最近更新 更多