2017-05-28 28 views
0
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?

+1

能否請您重新制定你的問題?除了之前寫入的位置之外,您是否希望在每個字段中寫入新的字段? –

+0

@SebastianWalla,不,我只是想改變位置'new'元素二維數組,以前檢查出可能的運動。這很難,因爲我無法得到如何適合每個細胞座標'x'和'y'。 – This4fun

回答

0

問題:如何通過循環檢查座標是否與另一個座標不同。

定義使用tuple座標的,然後比較tuples

RC = (1,4) 
RC2 = (1,0) 

if RC == RC2: 
    print('Equal') 
else: 
    print('Different') 

類型list是0基於 你有一個列表的列表。

RC座標系,一樣用Excel
ř==一個list == X ==柱

 A B C D 
C =>  0 1 2 3 
    ---------------------- 
R:0 | [0.0, 0.1, 0.2, 0.3] 
R:1 | [1.0, 1.1, 1.2, 1.3] 
R:2 | [2.0, 2.1, 2.2, 2.3] 
R:3 | [3.0, 3.1, 3.2, 3.3] 
內側 list ==ý==行
的C list指數==索引

使用相對

#arr[y][x] 

第一指數= y是印出在arr一個list的X,別名
第二個索引= x是使用y選擇的list中的索引,別名爲

+0

不明白這一點,你可以解釋一下嗎?它如何幫助? – This4fun

+0

@ This4fun:更新了我的答案[當有人回答我的問題時該怎麼辦?] https://stackoverflow.com/help/someone-answers – stovfl

相關問題