GoTo Next Iteration in For Loop in java
continue;
continue;
key word would start the next iteration upon invocation For Example
for(int i= 0 ; i < 5; i++){
if(i==2){
continue;
}
System.out.print(i);
}
This will print0134
See
- Document
How to skip iterations in a loop?
You are looking for continue
.
Skip iteration in For loop and repeat initialisation in Python
The if
statement is already skipping an iteration, so all you need is to repeat the initialization. You can do this by re-initializing the sum variable (a
) inside the outer loop instead of outside.
A = [2, 4, 3, 1]
s = []
for i in range(len(A)):
a = 0
for j in range(len(A)):
if i == j:
continue
else:
a += A[j]
s.append(a)
print(s)
Output:[8, 6, 7, 9]
How to skip few iterations in a for loop based on a if condition?
Skipping 4 steps after each meeting condition ((i % 300) == 0
) is equal to skipping 0
, 1
, 2
, and 3
. You can simply change the condition to skip all these steps with (i % 300) < 4
.
for i in range(0, 200000):
# (when 0 % 300 it meets this criteria)
if (i % 300) < 4: # Skips iteration when remainder satisfier condition
#if (i % 300) in (0,1,2,3): # or alternative skip condition
# whenever this condition is met skip 4 iterations forward
# now skip 4 iterations --- > (0, 1, 2, 3)
continue
# now continue from 4th iteration
print(i)
Skip iteration in loop if taking too long
You should run pd.read_sql in an another Thread, you can use this utility functions:
import time
from threading import Thread
class ThreadWithReturnValue(Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs={}, Verbose=None):
Thread.__init__(self, group, target, name, args, kwargs)
self._return = None
def run(self):
print(type(self._target))
if self._target is not None:
self._return = self._target(*self._args,
**self._kwargs)
def join(self, *args):
Thread.join(self, *args)
return self._return
def call(f, *args, timeout = 5, **kwargs):
i = 0
t = ThreadWithReturnValue(target=f, args=args, kwargs=kwargs)
t.daemon = True
t.start()
while True:
if not t.is_alive():
break
if timeout == i:
print("timeout")
return
time.sleep(1)
i += 1
return t.join()
def read_sql(a,b,c, sql="", con=""):
print(a, b, c, sql, con)
t = 10
while t > 0:
# print("t=", t)
time.sleep(1)
t -= 1
return "read_sql return value"
conn = "conn"
t = "t"
print(call(read_sql, "a", "b", "c", timeout=10, sql=f''' select * from [DB].[SCHEMA].[{t}] ''', con=conn))
I got help from this answer.by those functions:
for t in tablelist:
df = call(pd.read_sql, timeout=yourTimeOutInSeconds , sql=f''' select * from [DB].[SCHEMA].[{t}] ''', con=conn)
if df:
df.to_csv(path, index=None)
Skip multiple iterations in a loop
I'd iterate over iter(z)
, using islice
to send unwanted elements into oblivion... ex;
from itertools import islice
z = iter([1, 2, 3, 4, 5, 6, 7, 8])
for el in z:
print(el)
if el == 4:
_ = list(islice(z, 3)) # Skip the next 3 iterations.
# 1
# 2
# 3
# 4
# 8
OptimizationIf you're skipping maaaaaaany iterations, then at that point
list
ifying the result will become memory inefficient. Try iteratively consuming z
:for el in z:
print(el)
if el == 4:
for _ in xrange(3): # Skip the next 3 iterations.
next(z)
Thanks to @Netwave for the suggestion.If you want the index too, consider wrapping
iter
around an enumerate(z)
call (for python2.7.... for python-3.x, the iter
is not needed).z = iter(enumerate([1, 2, 3, 4, 5, 6, 7, 8]))
for (idx, el) in z:
print(el)
if el == 4:
_ = list(islice(z, 3)) # Skip the next 3 iterations.
# 1
# 2
# 3
# 4
# 8
Related Topics
Running Multiple Bash Commands with Subprocess
Reading Tar File Contents Without Untarring It, in Python Script
Basic Program to Convert Integer to Roman Numerals
How to Read a Response from Python Requests
Does Python Evaluate If's Conditions Lazily
Representing and Solving a Maze Given an Image
How Do Threads Work in Python, and What Are Common Python-Threading Specific Pitfalls
Django: Adding "Nulls Last" to Query
Building a Minimal Plugin Architecture in Python
Typeerror: Got Multiple Values for Argument
How to Set Opacity of Background Colour of Graph with Matplotlib
Broken References in Virtualenvs
Python Element-Wise Tuple Operations Like Sum
First Python List Index Greater Than X