1

I have a following loop in C:

for (i = 0, j = nvert-1; i < nvert; j = i++) {
//do something
}

I want to write this loop in python. I know that in Python the loop uses the range(start,end) format, so I think I should make this somehow as follows:

for i in range(0,nvert):
    for j in range(???):
        #do something

so my problems are with this:

  • how can I determine the range of j?
  • how can I do the j=i++ trick in python (so this loop would act the same as the C-loop above)?
2
  • You can't do the j=i++ trick in Python unfortunately Commented May 2, 2013 at 7:54
  • 2
    how do you use the indexes? Often in Python, you can drop indexes and iterate over container's items directly e.g., instead of for (i = 0; i < n; ++i) a[i]; in C, you could write for item in a: item in Python. If you want a circular buffer; look at collections.deque() Commented May 2, 2013 at 8:09

1 Answer 1

3

You don't need the second loop. You can simply include it into the loop:

j = nvert - 1
for i in range(0, nvert):
      # do stuff
      j = i

note that you can simply use range(nvert) instead of range(0, nvert)

Sign up to request clarification or add additional context in comments.

3 Comments

@Blender I removed the semicolons, I think the 0, was just copying the OP
as @Blender said you can tell OP range(nvert) is sufficient
@jamylak can you fix it? Actually, I am not a python programer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.