ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 06 Jul 2018 06:49:10 -0500How to skip a single loop iterationhttps://ask.sagemath.org/question/42837/how-to-skip-a-single-loop-iteration/I have the following code:
for k in range(9):
if k != 3:
print(k)
else:
print(20)
# i want to skip the next iteration
So I want to get: 0,1,2,20,5,6,7,8, here I skip the fourth iteration.
I already try to use the command next(), but it doesn't give what I want
Thu, 05 Jul 2018 19:30:36 -0500https://ask.sagemath.org/question/42837/how-to-skip-a-single-loop-iteration/Answer by vdelecroix for <p>I have the following code:</p>
<pre><code> for k in range(9):
if k != 3:
print(k)
else:
print(20)
# i want to skip the next iteration
</code></pre>
<p>So I want to get: 0,1,2,20,5,6,7,8, here I skip the fourth iteration.
I already try to use the command next(), but it doesn't give what I want</p>
https://ask.sagemath.org/question/42837/how-to-skip-a-single-loop-iteration/?answer=42847#post-id-42847The following
R = iter(range(9))
for r in R:
if r == 3:
a = next(R) # remove the next element from the iterator
print(r)
produces
0
1
2
3
5
6
7
8
Fri, 06 Jul 2018 06:49:10 -0500https://ask.sagemath.org/question/42837/how-to-skip-a-single-loop-iteration/?answer=42847#post-id-42847Answer by joaoff for <p>I have the following code:</p>
<pre><code> for k in range(9):
if k != 3:
print(k)
else:
print(20)
# i want to skip the next iteration
</code></pre>
<p>So I want to get: 0,1,2,20,5,6,7,8, here I skip the fourth iteration.
I already try to use the command next(), but it doesn't give what I want</p>
https://ask.sagemath.org/question/42837/how-to-skip-a-single-loop-iteration/?answer=42838#post-id-42838Is that all you want?
One way to do that.
for k in range(9):
if k != 3:
if k != 4:
print(k)
else:
pass
else:
print(20)
# i want to skip the next iterationFri, 06 Jul 2018 03:23:48 -0500https://ask.sagemath.org/question/42837/how-to-skip-a-single-loop-iteration/?answer=42838#post-id-42838Comment by vdelecroix for <p>Is that all you want?</p>
<p>One way to do that.</p>
<pre><code>for k in range(9):
if k != 3:
if k != 4:
print(k)
else:
pass
else:
print(20)
# i want to skip the next iteration
</code></pre>
https://ask.sagemath.org/question/42837/how-to-skip-a-single-loop-iteration/?comment=42843#post-id-42843the
else:
pass
can be removed... the whole thing is equivalent to
for k in range(9):
if k != 3:
print(k)
elif k != 4:
print(20)Fri, 06 Jul 2018 05:19:26 -0500https://ask.sagemath.org/question/42837/how-to-skip-a-single-loop-iteration/?comment=42843#post-id-42843Answer by dan_fulea for <p>I have the following code:</p>
<pre><code> for k in range(9):
if k != 3:
print(k)
else:
print(20)
# i want to skip the next iteration
</code></pre>
<p>So I want to get: 0,1,2,20,5,6,7,8, here I skip the fourth iteration.
I already try to use the command next(), but it doesn't give what I want</p>
https://ask.sagemath.org/question/42837/how-to-skip-a-single-loop-iteration/?answer=42842#post-id-42842Depending on the real need, the one or the other of the following code snippets may be preferable.
(1) Arrange the range.
R = [0..2] + [20] + [5..8]
for r in R:
print r
(2) Define a function that does the right thing for the right `k`:
def f(k):
if k == 3: print 20
elif k == 4: pass
else: print k
for k in [0..8]:
f(k)
(3) Do the same as above without function.
for k in [0..8]:
if k == 4: continue
elif k == 3: print 20
else: print k
(4) If in the real application we have no good control of the position, after that we skip, we can use a flag, `skipNext` say...
def myCondition(k):
if k == 3:
return True
return False
nextSkip = False
for k in range(9):
oldSkip = nextSkip
nextSkip = myCondition(k)
if oldSkip:
continue
if nextSkip:
print 20
else:
print k
You see how hard is to guess the "needed solution".
The question is a pure python question, consider reviewing data structures in python and logical ramification possibilites.Fri, 06 Jul 2018 05:11:36 -0500https://ask.sagemath.org/question/42837/how-to-skip-a-single-loop-iteration/?answer=42842#post-id-42842