Reputation: 55
I have two for loops like
jj = 0
for ii in range(filename_list.__len__()):
jj += 1
for jj in range(filename_list.__len__()):
if ii != jj:
print(str(ii) + " " + str(jj) + "\n")
print("=" * 5)
every time the ii
for loop fully executes I want the next jj
for loop to execute starting at the index +1 of what it was before.
currently, the output looks like
0 1
0 2
=====
1 0
1 2
=====
2 0
2 1
=====
but it should look more like
0 1
0 2
=====
1 2
thus for every iteration of the ii
for loop there should be one less iteration for the jj
loop.
Upvotes: 2
Views: 234
Reputation: 1656
while J+=1
does increment j, the new value is ignored, as in the next line you're telling Python to start j at zero (range(x)
is a range starting at x and ending at x-1)
How about:
for ii in range(filename_list.__len__()):
for jj in range(ii + 1, filename_list.__len__()):
if ii != jj:
print(str(ii) + " " + str(jj) + "\n")
Upvotes: 0
Reputation: 17322
the inner for loop you could start from ii, since ii it is incremented with 1 after each loop of the inner for:
for ii in range(filename_list.__len__()):
for jj in range(ii, filename_list.__len__()):
if ii != jj:
print(str(ii) + " " + str(jj) + "\n")
print("=" * 5)
output:
0 1
0 2
=====
1 2
Upvotes: 0
Reputation: 58
you can insert the starting point in the second range like below:
jj = 0
n=3
for ii in range(n):
jj += 1
for jj in range(ii,n):
if ii != jj:
print(str(ii) + " " + str(jj) + "\n")
print("=" * 5)
I change the filename_list.__len__()
into n
you can see I add the ii in the second range that means start the jj from ii, not from zero
Upvotes: 0
Reputation: 1047
You're also changing jj
in your inner loop's iteration. Try this:
jj = 0
for ii in range(filename_list.__len__()):
for jj in range(ii+1, filename_list.__len__()):
print(str(ii) + " " + str(jj) + "\n")
print("=" * 5)
Upvotes: 2