Python Numpy:如何滚动1“;“世界其他地区”;在一组数组中
我正在创建一个2d numpy数组,下面是一个简化的示例:Python Numpy:如何滚动1“;“世界其他地区”;在一组数组中,python,arrays,numpy,Python,Arrays,Numpy,我正在创建一个2d numpy数组,下面是一个简化的示例: COL01 = np.array(["A", "D", "G"]) COL02 = np.array(["B", "E", "H"]) COL03 = np.array(["C", "F", "I"]) GRID = np.array([[COL01], [COL02], [COL03]]) 我在代码中传递网格。我希望能够通过仅滚动组成其组件行的一个数组来修改网格。例如,我想将GRID传递到一个函数中,该函数具有要滚动的行号和位置数
COL01 = np.array(["A", "D", "G"])
COL02 = np.array(["B", "E", "H"])
COL03 = np.array(["C", "F", "I"])
GRID = np.array([[COL01], [COL02], [COL03]])
我在代码中传递网格。我希望能够通过仅滚动组成其组件行的一个数组来修改网格。例如,我想将GRID传递到一个函数中,该函数具有要滚动的行号和位置数,然后返回结果
如何独立滚动单行?我试着从这里找到答案:
[,但我想不出如何使该答案适应我的问题。此代码可能会解决您的问题 REV_GRID是网格的滚动版本
COL01 = ["A", "D", "G"]
COL02 = ["B", "E", "H"]
COL03 = ["C", "F", "I"]
GRID = []
GRID.append(COL01)
GRID.append(COL02)
GRID.append(COL03)
REV_GRID = []
for col in GRID:
REV_GRID.append(col[::-1])
print GRID
print REV_GRID
如果它不能解决您的问题,请告诉我。我们将对此进行处理。您可以通过选择要操作的行并使用来完成此操作 假设我们要将第一行向右滚动一个位置:
import numpy as np
grid = np.array([['A', 'D', 'G'],
['B', 'E', 'H'],
['C', 'F', 'I']])
grid[0] = np.roll(grid[0], 1)
print grid
这将产生:
[['G' 'A' 'D']
['B' 'E' 'H']
['C' 'F' 'I']]
Roll the second row one place
[['A' 'D' 'G']
['H' 'B' 'E']
['C' 'F' 'I']]
Roll the second row two places
[['A' 'D' 'G']
['E' 'H' 'B']
['C' 'F' 'I']]
Roll the second row three places
[['A' 'D' 'G']
['B' 'E' 'H']
['C' 'F' 'I']]
请注意,我们正在修改原始数组
您应该决定是就地操作阵列(修改原始阵列),还是每次复制一个阵列。根据您的决定,重复调用会产生不同的效果:
import numpy as np
def independent_roll_inplace(arr, ind, amount):
arr[ind] = np.roll(arr[ind], amount)
def independent_roll_copy(arr, ind, amount):
arr = arr.copy()
arr[ind] = np.roll(arr[ind], amount)
return arr
grid = np.array([['A', 'D', 'G'],
['B', 'E', 'H'],
['C', 'F', 'I']])
作为区别的一个例子,如果我们每次复制一份,我们就开始对原始网格进行“刷新”。重复调用对原始网格没有影响:
print 'Roll the second row one place'
print independent_roll_copy(grid, 1, 1)
print 'Roll the second row two places'
print independent_roll_copy(grid, 1, 2)
print 'Roll the second row three places'
print independent_roll_copy(grid, 1, 3)
这将产生:
[['G' 'A' 'D']
['B' 'E' 'H']
['C' 'F' 'I']]
Roll the second row one place
[['A' 'D' 'G']
['H' 'B' 'E']
['C' 'F' 'I']]
Roll the second row two places
[['A' 'D' 'G']
['E' 'H' 'B']
['C' 'F' 'I']]
Roll the second row three places
[['A' 'D' 'G']
['B' 'E' 'H']
['C' 'F' 'I']]
但是,如果我们每次都修改原始文件,我们将通过多次滚动一个位置得到相同的结果:
for _ in range(3):
print 'Roll the first row one place, modifying the original'
independent_roll_inplace(grid, 0, 1)
print grid
屈服:
Roll the second row one place, modifying the original
[['A' 'D' 'G']
['H' 'B' 'E']
['C' 'F' 'I']]
Roll the second row one place, modifying the original
[['A' 'D' 'G']
['E' 'H' 'B']
['C' 'F' 'I']]
Roll the second row one place, modifying the original
[['A' 'D' 'G']
['B' 'E' 'H']
['C' 'F' 'I']]
在构建
GRID
之前传递列并滚动它们,或者将GRID
分解回列,滚动一列,然后重建GRID
,感谢您的建议,但这会移动每行中的值。我希望能够任意选择一行并仅滚动该数组的值。这可以在上面的例子中,假设您想要滚动COL02,可以通过以下方式完成:COL02=GRID[1]REV_COL02=COL02[::-1]COL02=[“B”,“E”,“H”]REV_COL02=[“H”,“E”,“B”]我希望它能解决你的问题。如果我误解了你的问题,请告诉我。我选择了乔·金顿的答案,因为我不必解构和重新组装网格。