Reputation: 395
I have a case where I need 2 for loops (i and k) as shown below. And I want to continue with the inner loop after I left it.
import numpy as np
X = [[12, 11, 1], [1,2,3]]
mu = [1, 2, 3]
sublist = []
for i in range(0, 4):
for k in range(0, 3):
subtr = X[i] - mu[k]
sublist.append(subtr)
# leaving the loop k to calc argmin
agmin = np.argmin(sublist)
C.append(agmin)
# Now I want to get back to the inner loop (k) to continue #further calculation, but obviously will result an error.
np.dot((C[i] == k),X[i])
What is the best way to deal with such cases?
Upvotes: 0
Views: 671
Reputation: 8954
do everything you need to do in the inner loop, before you leave the inner loop. Below is a slightly modified version of your code:
import numpy as np
X = [[12, 11, 1], [1,2,3]]
mu = [1, 2, 3]
sublist = []
C = #whatever C should be initialized to
for i in range(0, 4):
for k in range(0, 3):
subtr = X[i] - mu[k]
sublist.append(subtr)
# calculate agmin (argmin) once per inner loop, at end
if k == 2:
agmin = np.argmin(sublist)
C.append(agmin)
# not sure what this line does, but do it inside the inner loop since it
# needs k. (I'm guessing you really want some_var = np.dot(...) )
np.dot((C[i] == k),X[i])
Upvotes: 2