Python for循环在第一次迭代后停止工作

时间:2017-04-21 09:19:43

标签: python python-3.x loops for-loop

我有一个for循环,在第一次迭代后返回零。我把它打印出来,所以我知道它实际上是循环的,但由于某种原因它似乎并没有在第一次迭代后调用我的函数new_lattice。

N=[4,8,16,20,25]
for i,j in enumerate(N):
    print(i)
    init_lattice=np.ones((j,j))
    #new_lattice is a function that returns multiple lists 
    data=new_lattice(init_lattice,j)
    print (data[1])

打印应打印出函数返回的一个列表,但除第一次迭代外,列表的所有元素都为零。如果我在循环外调用N =任意值的函数,那么元素不为零,所以它似乎是循环问题。我有另一个python文件,它有完全相同的循环,但是那个有效,所以我不明白为什么不这样做!

以下是完整的代码,包括函数:

import numpy as np
from numpy import random as rn
import matplotlib.pyplot as plt

temp1= np.arange(2.0, 3.0, 0.1)
temp=enumerate(temp1)
number_of_sweeps=200
eqm_sweeps=50

def new_lattice(lattice,L):
    delta_E=np.zeros((L,L))
    mag=np.zeros(number_of_sweeps)
    mag1=np.zeros(len(temp1))
    mag2=np.zeros(len(temp1))
    mag4=np.zeros(len(temp1))
    for n, T in temp:
        for sweep in range(number_of_sweeps+eqm_sweeps):
            for i in range(L):
                for j in range(L):
                    Si=lattice[i,j]
                    sum_Sj=lattice[i,(j+1)%L]+lattice[(i+1)%L,j]+lattice[i,(j-1)%L]+lattice[(i-1)%L,j]
                    delta_E[i,j]=2*Si*sum_Sj

                    if delta_E[i,j] > 0.0 and rn.random() <  np.exp(-1*delta_E[i,j]/(T)):
                        lattice[i,j] *= -1
                    elif delta_E[i,j] <= 0.0:
                        lattice[i,j] *= -1
            if sweep>=eqm_sweeps:
                mag[sweep-eqm_sweeps]=abs(np.sum(lattice))
                mag1[n]=np.sum(mag)/number_of_sweeps
                mag2[n]=np.sum(mag**2)/((L**2)*number_of_sweeps)
                mag4[n]=np.sum(mag**4)/((L**2)*number_of_sweeps)

    return mag1, mag2,mag4,lattice

代码使用Metropolis算法来模拟Ising模型。 输出如下:

    0
[ 3323.37   3225.43   2912.865  2740.01   2392.66   2266.455  1964.165
  1804.22   1595.68   1317.135]
1
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
2
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
3
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
4
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]

但我希望最后4个列表中包含非零元素。

1 个答案:

答案 0 :(得分:1)

问题是你在函数外部存储枚举并循环。

temp=enumerate(temp1)
for n, T in temp:

在j = 4的第一次函数调用之后,枚举临时排气和“for n,T in temp”不再起作用。

在new_lattice函数中使用枚举,以便在每次函数调用时生成枚举对象。

for n, T in enumerate(temp1):

示例输出:

0
(array([ 15.07,  14.17,  14.03,  14.19,  12.95,  11.76,  11.07,  10.71,
        11.14,  11.01]), array([ 14.43625,  13.16625,  12.90875,  13.02875,  11.32125,   9.9925 ,
         8.89625,   8.52875,   8.89   ,   8.88625]), array([ 3479.785,  3070.425,  2948.195,  2955.275,  2419.065,  2109.61 ,
        1747.325,  1640.615,  1700.32 ,  1730.785]), array([[ 1., -1.,  1., -1.],
       [ 1., -1., -1.,  1.],
       [-1., -1., -1., -1.],
       [-1.,  1., -1.,  1.]]))
1
(array([ 57.34,  54.44,  52.75,  42.99,  43.95,  35.48,  31.77,  31.81,
        25.06,  22.71]), array([ 52.979375 ,  48.47     ,  45.4803125,  33.8340625,  33.2053125,
        24.343125 ,  20.0590625,  20.0928125,  13.17125  ,  11.6246875]), array([ 190270.6775 ,  166042.28   ,  146420.65625,  100278.72625,
         87279.49125,   58296.1425 ,   41876.73125,   42170.46125,
         20914.28   ,   18993.91375]), array([[ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1., -1.,  1.],
       [-1., -1.,  1.,  1., -1.,  1., -1.,  1.],
       [-1., -1.,  1.,  1., -1., -1., -1.,  1.],
       [-1., -1.,  1.,  1.,  1.,  1., -1., -1.],
       [-1., -1.,  1.,  1.,  1.,  1.,  1., -1.],
       [-1.,  1.,  1.,  1., -1., -1., -1., -1.],
       [ 1.,  1.,  1.,  1.,  1., -1., -1.,  1.]]))
......
......
相关问题