Reputation: 35681
Can I do something like this in Python?
for (i = 0; i < 10; i++):
if someCondition:
i+=1
print i
I need to be able to skip some values based on a condition
EDIT: All the solutions so far suggest pruning the initial range in one way or another, based on an already known condition. This is not useful for me, so let me explain what I want to do.
I want to manually (i.e. no getopt) parse some cmd line args, where each 'keyword' has a certain number of parameters, something like this:
for i in range(0,len(argv)):
arg = argv[i]
if arg == '--flag1':
opt1 = argv[i+1]
i+=1
continue
if arg == '--anotherFlag':
optX = argv[i+1]
optY = argv[i+2]
optZ = argv[i+3]
i+=3
continue
...
Upvotes: 21
Views: 34787
Reputation: 1
increment = 4 #say
for i in range(n):
#write your code here
n = n + increment
This might be the simple solution to the problem if you just want to iterate through the array by skipping 4 members
Upvotes: -1
Reputation: 606
You can ensure that an index is incremented within a try...finally
block. This solve the common problem of wanting to continue
to the next index without having to copy/past i += 1
everywhere. Which is one of the main advantages the C-like for
loop offers.
The main disadvantage to using a try...finally
is having to indent your code once more. but if you have a while
loop with many continue
conditions its probably worth it.
This example demonstrates that i
still gets incremented in the finally
block, even with continue
being called. If i
is not incremented its value will remain even forever, and the while
loop will become infinite.
i = 0
while i < 10:
try:
print(i)
if i % 2 == 0:
continue
finally:
i += 1
without it you would have to increment i
just before calling continue
.
i = 0
while i < 10:
print(i)
if i % 2 == 0:
i += 1 # duplicate code
continue
i += 1
Upvotes: 0
Reputation: 329
for (i = 0; i < 10; i++)
if someCondition:
i+=1
print i
In python would be written as
i = 0
while i < 10
if someCondition
i += 1
print i
i += 1
there you go, that is how to write a c for loop in python.
Upvotes: 14
Reputation: 314
You could first turn the argv list into a generator:
def g(my_list):
for item in my_list:
yield item
You could then step through the items, invoking the generator as required:
my_gen = g(sys.argv[1:]):
while True:
try:
arg = my_gen.next()
if arg == "--flag1":
optX = my_gen.next()
opyY = my_gen.next()
--do something
elif arg == "--flag2":
optX = my_gen.next()
optY = my_gen.next()
optZ = my_gen.next()
--do something else
...
except StopIteration:
break
Upvotes: 1
Reputation: 5555
Your problem seems to be that you should loop not raw parameters but parsed parameters. I would suggest you to consider to change your decision not to use standard module (like the others).
Upvotes: -1
Reputation: 132088
Yes, this is how I would do it
>>> for i in xrange(0, 10):
... if i == 4:
... continue
... print i,
...
0 1 2 3 5 6 7 8 9
EDIT
Based on the update to your original question... I would suggest you take a look at optparse
Upvotes: 24
Reputation: 7389
If you need to iterate over something, and need an index, use enumerate()
for i, arg in enumerate(argv):
...
which does the same as the questioner's
for i in range(0,len(argv)):
arg = argv[i]
Upvotes: -1
Reputation: 319821
There are two things you could do to solve your problem:
getopt
, or any other module then.or do more fragile own processing:
sys.argv.pop()
cmd = {}
while sys.argv:
arg = sys.argv.pop(0)
if arg == '--arg1':
cmd[arg] = sys.argv.pop(0), sys.argv.pop(0)
elif:
pass
print(cmd)
Upvotes: 8
Reputation: 1394
for i in xrange(0, 10):
if i % 3 == 0
continue
print i
Will only values which aren't divisible by 3.
Upvotes: -1
Reputation: 49946
You probably don't actually need the indices, you probably need the actual items. A better solution would probably be like this:
sequence = 'whatever'
for item in sequence:
if some_condition:
continue
do_stuff_with(item)
Upvotes: 2
Reputation: 5949
Strange way:
for x in (x for x in xrange(10) if someCondition):
print str(x)
Upvotes: 5
Reputation: 523494
You should use continue
to skip a value, in both C and Python.
for i in range(10):
if someCondition:
continue
print(i)
Upvotes: 3