arr = [['.' for i in range(4)] for j in range(4)]
for line, i in enumerate(arr):
for column, j in enumerate(i):
print(j, 'at column', column+1, 'line', line+1) # we can know which
# postition takes
# every element
如何退出循環,以檢查是否協調是另一個座標不同。如何訪問2d數組的每個元素並將其替換?
我想在決賽中能得到什麼:
僞代碼:
#arr[x][y]
arr[1][0] = 'new'
if arr[1][4] - arr[1][0] == 4: # i.e. coord are different by `y` on 4 pos
arr[1][4] = 'new'`
# Before || After
[[' ', 'new', ' ', ' '],|| [[' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' '], || [' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' '], || [' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' '], || [' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ']] || [' ', 'new', ' ', ' ']]
OR
#arr[x][y]
arr[0][0] = 'new'
if arr[3][0] - arr[0][0] == 3: # i.e. coord are different by `x` on 3 pos
arr[3][0] = 'new'`
# Before || After
[['new', ' ', ' ', ' '],|| [[' ', ' ', ' ', 'new'],
[' ', ' ', ' ', ' '], || [' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' '], || [' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' '], || [' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ']] || [' ', ' ', ' ', ' ']]
一定需要知道這在主力名單列表中需要的位置,但它如何在沒有numpy的循環之外使用本地python?
能否請您重新制定你的問題?除了之前寫入的位置之外,您是否希望在每個字段中寫入新的字段? –
@SebastianWalla,不,我只是想改變位置'new'元素二維數組,以前檢查出可能的運動。這很難,因爲我無法得到如何適合每個細胞座標'x'和'y'。 – This4fun