【问题标题】:Error in nested for loops (Python)嵌套 for 循环中的错误(Python)
【发布时间】:2009-09-28 15:05:01
【问题描述】:

我在以下代码中遇到错误。错误消息是"Error: Inconsistent indentation detected!"

s=[30,40,50]
a=[5e6,6e6,7e6,8e6,8.5e6,9e6,10e6,12e6]
p=[0.0,0.002,0.004,0.006,0.008,0.01,0.015,0.05,0.1,0.15,0.2]
j=0
b=0
x=0


for j in s:
    h=s[j]
    print "here is the first loop" +h
    for b in a:
           c=a[b]                                #too much indentation
           print"here is the second loop" +c     #too much indentation
           for x in p:                           #too much indentation
                k=p[x]
                print"here is the third loop" +k

如果有任何其他错误,如果有人能纠正我,我将非常感激。

谢谢。

/吉拉尼

【问题讨论】:

    标签: python syntax


    【解决方案1】:

    清理制表符和空格后(您应该只有制表符或空格),您需要修复循环:

    s = [30,40,50]
    a = [5e6,6e6,7e6,8e6,8.5e6,9e6,10e6,12e6]
    p = [0.0,0.002,0.004,0.006,0.008,0.01,0.015,0.05,0.1,0.15,0.2]
    
    for j in s:        # within loop j is being 30 then 40 then 50, same true for other loops
        print "here is the first loop" + j
        for b in a:
            print"here is the second loop" + b
            for x in p:
                print"here is the third loop" + x
    

    否则你会得到IndexError

    【讨论】:

      【解决方案2】:

      SilentGhost 是正确的——不像 Javascript 这样的语言,当你编写时

      s = [30, 40, 50]
      for j in s:
      

      那么 j 没有被赋值为 0、1 和 2——它被赋值为实际值 30、40 和 50。所以不用说,换行,

      h = s[j]
      

      事实上,如果你这样做,第一次通过循环,它将评估为

      h = s[30]
      

      对于三元素列表来说,这是超出范围的,你会得到一个 IndexError。

      如果你真的想用另一种方式来做——如果你真的需要索引和值,你可以这样做:

      s = [30, 40, 50]
      for j in range(len(s)):
          h = s[j]
      

      len(s) 为您提供 s 的长度(在本例中为 3),并且 range 函数为您创建一个新列表, range(n) 包含从 0 到 n-1 的整数。在这种情况下,range(3) 返回 [0, 1, 2]

      正如 SilentGhost 在 cmets 中指出的那样,这更像是 Pythonic:

      s = [30, 40, 50]
      for (j, h) in enumerate(s):
          # do stuff here
      

      enumerate(s) 按顺序返回三对 (0, 30)、(1, 40) 和 (2, 50)。这样,您就可以同时获得 s 和实际元素的索引。

      【讨论】:

      • 如果他需要索引和值,他应该使用enumerate
      【解决方案3】:

      您将变量初始化为零的三行与其余代码的缩进不同。甚至 stackoverflow 上显示的代码也表明了这一点。

      另外,请检查您没有混合制表符和空格。

      【讨论】:

      • Traceback(最近一次调用最后):文件“C:/Users/esyefak/Desktop/newloops.py”,第 6 行,在 h=s[j] IndexError: list index out范围现在这是我遇到的错误??
      【解决方案4】:

      我也认为这是一个制表符/空格混合问题。一些编辑器,如 Textmate,可以选择显示“不可见”字符,如制表符和换行符。编码时非常方便,尤其是在 Python 中。

      【讨论】:

        【解决方案5】:

        这部分:

         for b in a:
                   c=a[b] #缩进太多
        

        “c=a[b]”缩进了 8 个空格而不是 4 个。它只需要 4 个空格(即一个 python 缩进)。

        Ian 是对的,“for x in y”的语法与其他语言不同。

        list1 = [10, 20, 30] 对于 list1 中的项目: 打印项目

        输出将是:“10, 20, 30”,而不是“1, 2, 3”。

        【讨论】:

        • “一个 Python 缩进”是你想要的。只要缩进增加,Python 并不真正关心 增加多少
        • @chris 你是对的,对此感到抱歉。虽然视觉上令人困惑,但“太多缩进”在 python 的语法上是可以的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-12
        • 2012-10-10
        • 1970-01-01
        • 2012-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多