【问题标题】:Python for loop stops working after first iteration [closed]Python for循环在第一次迭代后停止工作[关闭]
【发布时间】:2017-04-21 09:19:43
【问题描述】:

我有一个 for 循环,它在第一次迭代后返回零。我让它打印出 i,所以我知道它实际上是在循环,但由于某种原因,它似乎没有在第一次迭代后调用我的函数 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])

print 应该打印出函数返回的列表之一,但除了第一次迭代之外,列表的所有元素都为零。如果我用 N=any 值在循环外调用函数,那么元素不为零,所以问题似乎是循环。我有另一个具有完全相同循环的 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 个列表包含非零元素。

【问题讨论】:

  • 我们没有您的new_lattice 功能,因此这不是一个可验证的完整示例。请提供足够的代码来重现您的错误。
  • 而且预期的输出也会很好
  • 您可以在new_lattice 中添加一个print 调用,看看它是否真的进入了那里。
  • 问题一定出在你的init_latticenew_lattice
  • 它说'bootstraps'没有定义

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


【解决方案1】:

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

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

在使用 j=4 进行第一次函数调用后,枚举 temp 耗尽,“for n, T in temp”不再起作用。

在 new_lattice 函数中使用 enumerate 以便在每次函数调用时生成一个 enumerate 对象。

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.]]))
......
......

【讨论】:

    最近更新 更多