Python's for loops are different. i gets reassigned to the next value every time through the loop.
The following will do what you want, because it is taking the literal version of what C++ is doing:
i = 0
while i < some_value:
if cond...:
i+=1
...code...
i+=1
Here's why:
in C++, the following code segments are equivalent:
for(..a..; ..b..; ..c..) {
...code...
}
and
..a..
while(..b..) {
..code..
..c..
}
whereas the python for loop looks something like:
for x in ..a..:
..code..
turns into
my_iter = iter(..a..)
while (my_iter is not empty):
x = my_iter.next()
..code..
Answer from jakebman on Stack OverflowPython's for loops are different. i gets reassigned to the next value every time through the loop.
The following will do what you want, because it is taking the literal version of what C++ is doing:
i = 0
while i < some_value:
if cond...:
i+=1
...code...
i+=1
Here's why:
in C++, the following code segments are equivalent:
for(..a..; ..b..; ..c..) {
...code...
}
and
..a..
while(..b..) {
..code..
..c..
}
whereas the python for loop looks something like:
for x in ..a..:
..code..
turns into
my_iter = iter(..a..)
while (my_iter is not empty):
x = my_iter.next()
..code..
There is a continue keyword which skips the current iteration and advances to the next one (and a break keyword which skips all loop iterations and exits the loop):
for i in range(10):
if i % 2 == 0:
# skip even numbers
continue
print i
A single python loop from zero to n to zero - Stack Overflow
How do you start a for loop at 1 instead of 0?
Need to set i as zero everytime the for loop is run in python - Stack Overflow
How can I implement " for (int i=0; i<str.size; i= i*5) " in python using for loop?
Videos
You can use itertools for that:
If you want to go to 1024 and back once, you can use:
from itertools import chain
for i in chain(range(0,1024),range(1024,0,-1)):
print(i)
In case you will need this quite often, you can use a function to generate the iterable:
def range_back(start,end):
return chain(range(start,end),range(end,start,-1))
and use it like:
for i in range_back(0,1024):
print(i)
Or if you want to do this an infinite amount of times:
from itertools import chain, cycle
for i in cycle(chain(range(0,1024),range(1024,0,-1))):
print(i) chain two iterables:
import itertools
for i in itertools.chain(range(1+n), reversed(range(n))):
do_whatever(i)
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>Try something like this instead:
import string
def getAvailableLetters(lettersGuessed):
return sorted(set(string.ascii_lowercase) - set(lettersGuessed))
lettersGuessed = ['e', 'i', 'k', 'p', 'r', 's']
getAvailableLetters(lettersGuessed)
for loops in Python have a lesser-known (and somewhat arcane) else expression that can be used to determine if the entire loop completed. It's useful for restarting a loop.
for j in range(โฆ):
while True:
for i in range(26):
if list1[i] == str4[i]:
list1.remove(list1[i])
break # Break out of the `for` loop to continue the `while`.
else:
# The `else` will not happen if we `break` in the `for` loop.
break # Finished the `for` loop; Break out of the `while`.
Hello,
I can't seem to google my way into the answer. What is the python syntax for a (i=0;i<array.length;i++) for lists? for x in list syntax doesn't seem to work as expected. The iteration is very important, and it seems kind of tacky to make a while loop. What am I missing?
You could use numpy to iterate through an array and print the sequence:
import numpy
for e in numpy.array([[[1,2],[1,2]],[[1,2],[1,2]]]).flatten(): print(e)
Just replace that list with your list.
The below explanation will be a little bit generic but hopefully after reading it you'll got the basic knowledge to solve your particular or more complex problem that deal with high-dimensional arrays.
ALLOCATION (GENERAL CASE)
Let's start by reviewing a simple way of allocating space for arrays in python:
1d array: lst = [0]*K0
2d array: lst = [0]*K1*K0
3d array: lst = [0]*K2*K1*K0
4d array: lst = [0]*K3*K2*K1*K0
...
Nd array: lst = [0]*Kn*Kn-1*...*K0
The most typical dimensional arrays would be (1D, 2D, 3D, 4D) and K0/K1/K2/K3 are known as width/height/depth/time respectively.
INDEXING (GENERAL CASE)
Once you've allocated space all that remains to know is how to index these arrays, well, let's try to find out how you'd compute the index array:
1d: index = x0
2d: index = x1*k0 + x0
3d: index = x2*k1*k0 + x1*k0 + x0
4d: index = x3*k2*k1*k0 + x2*k1*k0 + x1*k0 + x0
...
Nd: index_n = xn*kn-1*kn-2*...*k0 + index_n-1
The most typical dimensional arrays would be (1D, 2D, 3D, 4D), so usually x0/x1/x2/x3 are known as x/y/z/t respectively.
PARTICULAR CASES (ALLOCATION & INDEXING)
1d: lst = [0]*width
lst[x]
2d: lst = [0]*width*height
lst[y*width + x]
3d: lst = [0]*width*height*depth
lst[z*width*height + y*width + x]
4d: lst = [0]*width*height*depth*duration
lst[t*width*height*depth + z*width*height + y*width + x]
CONCLUSION
It seems you're dealing with 3d arrays so as explained above we know at this point that one way to allocate a 3d array in python could be done like:
lst = [0]*width*height*depth
and one possible way to index such array (either to write or read it) could be:
lst[z*width*height + y*width + x]
Hope the whole explanation helps to clarify a little bit more about your concerns about arrays, be in python or any other language, the underlying theory will be always the same and the only difference is there may be other more performant ways to allocate/index arrays on those languages, that's all.