【问题标题】:if statement nested in a for loopif 语句嵌套在 for 循环中
【发布时间】:2012-05-04 11:18:57
【问题描述】:
c = file('cluster_info.txt')
for i in zip(ys[-10:],result): # has ten elements
    for j in c.readlines():
        cluster = j.split(',')
        if q in cluster:
            m = (q[i],cluster[0])
            f = pylab.figure() # code for plot starts here
            for n, fname in enumerate(m):
                image=Image.open(fname).convert("L")
                arr=np.asarray(image)
                f.add_subplot(2, 1, n)  # this line outputs images on top of each other
                pylab.imshow(arr,cmap=cm.Greys_r)
            pylab.title("%s, Top:Predicted,Bottom:Observed" %i[0])
            pylab.show()
        else:
            continue

这是来自较大代码的 sn-p,我希望生成 10 个图像/绘图,但 python 不生成绘图。我想我没有将 if 和 else: 正确嵌套在 for 循环中。请告诉我这里出了什么问题。

【问题讨论】:

  • do-my-work-for-me 标签在哪里?

标签: python if-statement for-loop


【解决方案1】:

else: continue 的意图是什么?如果你想继续外循环(i),你最好使用else:break。否则你也可以删除else 分支

另外,i 似乎根本没有在您的循环中使用?不可能是对的

【讨论】:

  • 我需要继续遍历 j 中的循环,然后继续遍历 i 中的循环
  • 应该还有两个:最后?
  • 这个问题很难回答(实际上是投反对票的好候选人)。如果似乎q in cluster 从来都不是真的。尝试找出为什么会出现这种情况(每次迭代中qcluster 的值是什么)。另外,检查m 的值以查看n, fname 循环是否曾经执行过
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多