python - 如何将网格中的所有字母向左移动?

标签 python loops grid

所以我在一些有用的 stackoverflow 用户的帮助下编写了一个生成网格的代码,这就是......

import random
import string

for x in range(4):
    for y in range(4):
        print( '['+random.choice(string.ascii_letters)+']',end='' )
    print()

现在我想创建一个函数 moveleft(grid) 它将每列向左移动

例如,如果我的网格一开始看起来像这样..

[k][b][s][v]
[u][w][p][O]
[t][i][i][K]
[n][J][r][f]

然后执行moveleft(grid)后,这将是我的网格,

[v][k][b][s]
[O][u][w][p]
[K][t][i][i]
[f][n][J][r]

我应该写什么才能让 moveleft(grid) 发挥上面提供的示例的作用?

最佳答案

numpy.roll做这个。例如,

>>> import numpy as np
>>> grid = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> grid
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> np.roll(grid, shift=1, axis=1)
array([[3, 1, 2],
       [6, 4, 5],
       [9, 7, 8]])

关于python - 如何将网格中的所有字母向左移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72108629/

相关文章:

python - 打开pyxl : need the max number of rows in a column that has data in Excel

python - Flask 应用程序的 Celery 配置

CSS Columns Bug — 使用 :checked pseudo class 时在移动设备上中断

html - 可见的网格线与 CSS

html - 至少将布局设置为视口(viewport)高度

python - 来自 AppEngine 的 Ping

python - 通过 python win32com 检索 outlook 联系人

python - 计算 2 个列表之间的重复项

java - 计算字符串数组中字符的出现次数

r - dplyr Mutate 中的错误 - 遍历列以生成图形