[英]python for loop in matrix to assign values
我正在創建一個程序,該程序將創建一個網格,並且該程序將在您在輸入處分配的矩陣數組的位置超出其位置。
碼:
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時,它將停止遞增j
我在這個方面還很新,所以謝謝你的幫助
看起來您在第二個for循環中使用了相同的名稱。 您可以嘗試更改此方法嗎?
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”對象不可迭代
為什么沒有收到此錯誤?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.