2017-03-01 76 views
0

我正在創建一個程序,將創建一個網格和程序將 希望通過您在輸入分配的矩陣數組的位置。python循環矩陣分配值

代碼:

def onbekende_naam(hoogtes): 
    print(hoogtes) 
    i = 0 
    j = 0 
    pos1 = set() 

    for hoogtes_subs in hoogtes: 
     j = 0 
     for hoogtes in hoogtes: 
      print("i = " + str(i)) 
      print("j = " + str(j)) 
      pos1.add((i, j)) 
      print pos1 
      j += 1 
     i += 1 
     #pos1.add((i, j)) 

    return pos1 

#verwerking 
print (onbekende_naam(hoogtes)) 

輸入:

4 4 
1 2 3 4 
5 6 7 8 
9 1 2 3 
4 5 6 7 
12 1 

輸出:

[['1', '2', '3', '4'], ['5', '6', '7', '8'], ['9', '1', '2', '3'], ['4', '5', '6', '7']] 
i = 0 
j = 0 
set([(0, 0)]) 
i = 0 
j = 1 
set([(0, 1), (0, 0)]) 
i = 0 
j = 2 
set([(0, 1), (0, 0), (0, 2)]) 
i = 0 
j = 3 
set([(0, 1), (0, 3), (0, 0), (0, 2)]) 
i = 1 
j = 0 
set([(0, 1), (0, 3), (0, 0), (0, 2), (1, 0)]) 
i = 1 
j = 1 
set([(0, 1), (0, 0), (0, 2), (1, 0), (0, 3), (1, 1)]) 
i = 1 
j = 2 
set([(0, 1), (1, 2), (0, 0), (0, 2), (1, 0), (0, 3), (1, 1)]) 
i = 1 
j = 3 
set([(0, 1), (1, 2), (0, 0), (0, 2), (1, 3), (1, 0), (0, 3), (1, 1)]) 
i = 2 
j = 0 
set([(0, 1), (1, 2), (0, 0), (0, 2), (2, 0), (1, 3), (1, 0), (0, 3), (1, 1)]) 
i = 3 
j = 0 
set([(0, 1), (1, 2), (0, 0), (3, 0), (0, 2), (2, 0), (1, 3), (1, 0), (0, 3), (1, 1)]) 
set([(0, 1), (1, 2), (0, 0), (3, 0), (0, 2), (2, 0), (1, 3), (1, 0), (0, 3), (1, 1)]) 

,你可以看到它停止遞增Ĵ當I值是大於2

我很新在此非常感謝您的幫助

+0

請在您的問題中包含代碼_as格式的text_,並確保它是[mcve]。 – ForceBru

+0

Ha,kan je je代碼normaal複製pasten? – rmeertens

回答

0

看起來您在第二個循環中使用了相同的名稱。你可以嘗試,如果改變這個作品?

def onbekende_naam(hoogtes): 
    print(hoogtes) 
    i = 0 
    j = 0 
    pos1 = set() 

    for hoogtes_subs in hoogtes: 
     j = 0 
     for another_name_hoogtes in hoogtes: 
      print("i = " + str(i)) 
      print("j = " + str(j)) 
      pos1.add((i, j)) 
      print pos1 
      j += 1 
     i += 1 
     #pos1.add((i, j)) 

    return pos1 

#verwerking 
print (onbekende_naam(hoogtes)) 

另外:當我運行你的原代碼,我得到以下錯誤:

TypeError: 'int' object is not iterable

你爲什麼沒有得到這個錯誤?

+0

看看我的編輯。你甚至沒有使用數組中的值。無論如何:不要在將來使用重複的變量名稱;) – rmeertens

+0

非常感謝。 我以前有過這個錯誤,並修復它。 但這次總算沒有了。 (在我的第一個評論中,我正在查看你的代碼,而不是原來的那個地方,我的困惑來自這裏) – learningjoe

+0

太好了。祝你的項目好運。 – rmeertens