The reason why the python for loop cannot modify the loop variable is that range() is like an iterator. It will only output information and cannot modify the contents of the iterator. The C structure of python iterator is a pointer and a list of objects. Modifying the value of a loop object is a C way of thinking, and it is best not to use it when writing python code.
It is seriously not recommended to modify the iteration variable. In many cases, it is impossible to use it for random access. There are many other ways to do this. Using Python's for-comprehension and some reduce methods can solve it efficiently and stably.
You should use list parsing and other methods to make the required modifications in advance. List parsing performance is very good, and for is only used for other operations.
You can modify the value of i, but the for statement will reassign i after each loop. So the question you ask is not whether you can modify i, but the behavior of the iterator. The answer is no.
You can use while, or use a generator:
def incSeq(seq):
start = 0
for i in xrange(1, len(seq)):
if seq[i] < seq[i-1]:
yield start, i - start
start = i
maxIncSeq = reduce(lambda x,y: x if x[1]>y[1] else y, incSeq(seq))
Get the longest increasing substring length and starting position, time complexity O(n).
Why do you need to modify the value of the loop variable? Doing so may cause problems. It is recommended to use other methods to achieve this, such as the while loop mentioned by several people above.
Modify the for statement to use a loop statement.
Why does the longest continuous increasing subsequence need to modify i
The code was written casually, it should be correct
The reason why the python for loop cannot modify the loop variable is that range() is like an iterator. It will only output information and cannot modify the contents of the iterator. The C structure of python iterator is a pointer and a list of objects. Modifying the value of a loop object is a C way of thinking, and it is best not to use it when writing python code.
It is seriously not recommended to modify the iteration variable. In many cases, it is impossible to use it for random access. There are many other ways to do this. Using Python's for-comprehension and some reduce methods can solve it efficiently and stably.
You should use list parsing and other methods to make the required modifications in advance. List parsing performance is very good, and for is only used for other operations.
You can modify the value of i, but the for statement will reassign i after each loop. So the question you ask is not whether you can modify i, but the behavior of the iterator. The answer is no.
You can use while, or use a generator:
Get the longest increasing substring length and starting position, time complexity O(n).
Why do you need to modify the value of the loop variable? Doing so may cause problems. It is recommended to use other methods to achieve this, such as the while loop mentioned by several people above.