Python,为什么i = + 1不会导致无限循环? [英] Python, why does i=+1 not result in an infinite loop?
问题描述
因此,前几天我因为错字而陷入一个问题.我没有使用i + = 1遍历嵌套循环,而是使用i = + 1.在开始打印步骤数并看到它是连续打印步骤1之前,我没有注意到这一点.因此,我得到的情节毫无意义.
So the other day I was stuck on a problem because of a typo. Instead of iterating through my nested loop with i += 1 I was using i=+1. I didn't notice this until I started printing the number of steps and saw it was printing step 1 continuously. The plots I was getting therefore weren't making any sense.
但是我不明白的是为什么我什么都没有,而且代码没有陷入无限循环中?另外,我应该只在完成步骤数的一半之后才开始计算数据,所以我根本不了解如何获得任何数据.还是我= + 1代表其他意思?我似乎在所有在线上都找不到关于i = + 1的太多信息
However what I don't understand is why I got any plots at all, and the code wasn't stuck in an infinite loop? Also, I should only have been calculating data after halfway through the number of steps, so I don't understand how I had any data at all. Or does i =+ 1 mean something else? I can't seem to find much information on i=+1 at all online
这是原始代码的精简版:
Here's a condensed version of the original code:
for temp in np.linspace(1.0,4.0,num=100):
energyarray = []
for step in np.arange(0, sw*2):
for i in range(n-1):
for j in range(n-1):
H_old = -J*matrix[i,j]*(matrix[i,j-1] + matrix[i,j+1] + matrix[i-1,j] + matrix[i+1,j])
H_new = J*matrix[i,j]*(matrix[i,j-1] + matrix[i,j+1] + matrix[i-1,j] + matrix[i+1,j])
del_H = H_old-H_new
if del_H >= 0:
matrix[i,j] = -matrix[i,j]
elif del_H < 0:
prob = np.exp((del_H)/(temp))
rand = random.random()
if rand < prob:
matrix[i,j] = -matrix[i,j]
else:
matrix[i,j] = matrix[i,j]
if step >= (sw):
Ene = EnergyCal(matrix)
energyarray.append(Ene)
step =+ 1
energy_sum = []
energy_sum = sum(energyarray)
plt.figure(10)
plt.plot(temp, energy_sum, 'ro')
plt.show()
推荐答案
Python for循环是基于迭代器的"for-each"循环.在每次迭代的开始重新分配迭代变量.换句话说,以下循环:
Python for-loops are iterator-based "for-each" loops. The iterating variable is reassigned at the beginning of each iteration. In other words, the following loop:
In [15]: nums = 1,2,5,8
In [16]: for num in nums:
...: print(num)
...:
1
2
5
8
等效于:
In [17]: it = iter(nums)
...: while True:
...: try:
...: num = next(it)
...: except StopIteration:
...: break
...: print(num)
...:
1
2
5
8
类似地,以下循环是等效的:
Similarly, the following loops are equivalent:
In [19]: for num in nums:
...: print("num:", num)
...: num += 1
...: print("num + 1:", num)
...:
...:
num: 1
num + 1: 2
num: 2
num + 1: 3
num: 5
num + 1: 6
num: 8
num + 1: 9
In [20]: it = iter(nums)
...: while True:
...: try:
...: num = next(it)
...: except StopIteration:
...: break
...: print("num:", num)
...: num += 1
...: print("num + 1:", num)
...:
num: 1
num + 1: 2
num: 2
num + 1: 3
num: 5
num + 1: 6
num: 8
num + 1: 9
注意,Python中不存在C样式的for循环,但是您可以随时编写while循环(c样式的for循环本质上是while循环的语法糖):
Note, C-style for-loops don't exist in Python, but you can always write a while-loop (c-style for loops are essentially syntactic sugar for while-loops):
for(int i = 0; i < n; i++){
// do stuff
}
等效于:
i = 0
while i < n:
# do stuff
i += 1
请注意,不同之处在于,在这种情况下,迭代取决于i
,# do stuff
中的任何修改i
的内容都会影响迭代,而在前一种情况下,迭代取决于 iterator .注意,如果我们确实修改了迭代器,则会影响迭代:
Note, the difference is that in this case, iteration depends on i
, anything in # do stuff
that modifies i
will impact iteration, whereas in the former case, iteration depends on the iterator. Note, if we do modify the iterator, then iteration is impacted:
In [25]: it = iter(nums) # give us an iterator
...: for num in it:
...: print(num)
...: junk = next(it) # modifying the iterator by taking next value
...:
...:
1
5
这篇关于Python,为什么i = + 1不会导致无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!