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
Answer from Inbar Rose on Stack Overflowwhat 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
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.
Python for loop start counter initialization - Stack Overflow
python - Initial value of a "for" loop - Stack Overflow
How do you start a for loop at 1 instead of 0?
Why don't we have to initialize the variable of a for loop?
The for index in range(len(list)) loop executes the loop body with index first set to 0, then 1, then 2, etc. up to len(list) - 1. The previous value of index is ignored and overwritten. If you want index to start at iteration + 1, use the 2-argument form of range:
for index in range(iteration + 1, len(list)):
You really should be using enumerate for stuff like this, as you can loop through the index and the value at the same time (which will save you the hassle of using two for-loops).
for i, j in enumerate(list):
print i, j
Your inner loop is overriding the variable index that you defined in the first loop.
I am pulling data from a dictionary and appending it to a list, but it appears that dictionary keys start at 1, not 0.
We have been taught to use for loops for this kind of task, but they initialize at 0. Can I start at 1? If not, what is an alternative? Thanks!
for i in range(len(dict)):
<code goes here>Straightforward way is following:
def funct_1(x):
y = x + 1
return y
def funct_2(y):
z = y + 1
return z
def main(x):
y = funct_1(x)
z = funct_2(y)
return z
state = 40
for i in range(10):
state = main(state)
This should work: In the first iteration you have your default value of x and in all iterations after that x is the output of funct_2().
x = init_state
def main():
y = funct_1(x)
x = funct_2(y)
for i in range(10):
main()