4

I've started learning Python a few days ago and I ran into a program while coding something.

Here's what I want to do using C++ code

number = SOME_NUMBER;
while(1) {
    for(int i=number; i<sizeOfArray; i++) {
        // do something
    }
    number = 0;
}

Basically, for the very first iteration of my for loop, I want to start i at number. Then for every other time i go through the for loop, I want to start it at 0.

My kind of hacky idea that I can think of right now is to do something like:

number = SOME_NUMBER
for i in range(0, len(array)):
    if i != number:
        continue
    // do something

while True:
    for i in range(0, len(array)):
        // do something

Is this the best way or is there a better way?

4
  • This depends on what // do something is. Commented Dec 27, 2012 at 11:20
  • Looping through lists by index makes no sense in Python, it is slow and doesn't read well - loop over the values instead. Commented Dec 27, 2012 at 11:44
  • Umm - you've already got answers - but I would write for(;;) instead of while(1) in your C++ code - I believe that's the way to write "forever"... Commented Dec 27, 2012 at 11:51
  • if you have an answer that solves your issue, you can mark is as accepted. Commented Dec 27, 2012 at 12:40

3 Answers 3

7

what is the problem with this?

starting_num = SOME_NUMBER
while True:
    for i in xrange(starting_num, len(array)):
        # do code
    starting_num = 0

it does exactly what you want.

however, i think there are better ways to do things especially if the solution seems "hacky".

if you gave an idea of what you wanted to do, maybe there is a better way

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

1 Comment

Oh that is true, I never thought about putting starting_num right in xrange.
2

I don't see why you couldn't just do the same thing you are in C:

number = SOME_NUMBER
while True:
    for i in range(number, len(array)):
        # do something
    number = 0

BTW, depending on which version of Python you're using, xrange may be preferable over range. In Python 2.x, range will produce an actual list of all the numbers. xrange will produce an iterator, and consumes far less memory when the range is large.

3 Comments

what is the difference between xrange and range?
I updated my answer, but xrange will produce an iterator, while range will produce a list. In Python 3, range produces an iterator.
if the range is really small though, 2-4, is it still better to use xrange?
1

In Python, stepping over a collection in the traditional sense is not ideal. The ability to loop - to iterate - over an object is controlled by the object, so you don't need to manually step through counters as you would in the for loop in C++.

As I understand it, what you are trying to do here is execute the same piece of code over each item in a list (there are no arrays in Python), a multiple number of times.

To do that:

def whatever_function(foo):
   # some code here that works on each item on the list
   # foo is an item of the list

while True:
   map(whatever_function, some_list)

2 Comments

hmm well my program is a card game that just looks through each person's turn.
originally, I had it so that it was while game is still running: for player in players: do all this but i changed it to indexes so I could keep track of what player "last made a play" by just storing the player index. a round could last through several for loops and players could pass on their turn. If on one iteration of a for loop, the person turned out to have made a move prior (by changing if the i == last_played_moved_index) It means everyone passed on their turn. then i'd reset everything so that a fresh round is started

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.