data = [
[0,1,2],
[3,4,5]
]
for row in data:
for item in row:
print (item)
data = [
[0,1,2],
[3,4,5]
]
for i in range(len(data)):
for j in range(len(data[i])):
print (data[i][j])
for i in range(len(data)):
for j in range(len(data[i])):
print (data[i][j])
j == len(data[i])
; i still zero).
for i in range(len(data)):
for j in range(len(data[i])):
print (data[i][j])
i j
0 0
0 1
0 2
1 0
1 1
1 2
for i in range(len(data)):
for i in range(len(data[j])):
data[j][i] = 10
for i in range(len(data)):
for i in range(len(data[j])):
data[j][i] = 10
len(data[j]))
Looping through to the wrong dimension length. This is very common if the lengths are hard-wired in, so avoid that.
for i in range(len(data)):
for i in range(len(data[j])):
data[j][i] = 10
for i
Cutting and pasting your outer loop to make your inner loop, and forgetting to change part of the variable use; here, the inner increment should be to j.
for i in range(len(data)):
for i in range(len(data[j])):
data[j][i] = 10
data[j][i]
Switching the indices the wrong way round. This should be data[i][j]
. With an non-square array, this will result in trying to read off one side of the array and the program will break. Worse, with a square array, your data will silently be transposed.
print ("a", end=",")
print ("b", end=",")
print ("c")
a,b,c
for i in range(len(data)):
# Line here done every outer loop
for j in range(len(data[i])):
# Line here done every inner loop
data[i][j] = 10
# Line here done every outer loop
for i in range(len(data)):
for j in range(len(data[i])):
print (data[i][j], end=",")
print ("")
for i in range(len(data)):
for j in range(len(data[i])):
data[i][j] = 10
for i in range(len(dataA)):
for j in range(len(dataA[i])):
dataA[i][j] = dataB[i][j]
for i in range(len(dataA)):
for j in range(len(dataA[i])):
dataA[i][j] = dataB[i-1][j-1]
Wrap boundaries: Suitable for modelling abstract landscapes Only process as many cells as you can: Suitable for modelling non- abstract landscapes Only process cells that can have complete processing: Suitable for image processing |
data = [
[0,1],
[2,3],
[4,5]
]
for a,b in data:
print (str(a) + " " + str(b))
a = [1,2,3,4,5]
b = [10,20,30,40,50]
z = zip(a,b)
d = {}
for t in z:
d[t[0]] = t[1]
print(d)
next(iterator[, default])
a = list(range(3))
it = iter(a)
for i in range(5):
print(next(it, "missing"))
0
1
2
missing
missing
reversed(seq)
a = list(range(3))
ri = reversed(a)
for i in ri:
print(i)
2
1
0
a.sort()
# Sorts list a.
# From then on, the list is sorted.
b = sorted(a)
# Copies list a, sorts it, and attaches the copy to b.