python无限循环无法实现遍历循环的功能_Python,为什么i = + 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.

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-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

Is equivalent to:

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

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

}

Is equivalent to:

i = 0

while i < n:

# do stuff

i += 1

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值