【问题标题】:Python 'list indices must be integers, not tuple"Python'列表索引必须是整数,而不是元组”
【发布时间】:2014-03-06 22:02:01
【问题描述】:

这两天我一直在努力解决这个问题。我是 python 和编程的新手,所以此类错误的其他示例对我没有太大帮助。我正在阅读列表和元组的文档,但没有找到任何有用的东西。任何指针将不胜感激。不一定要寻找答案,只是寻找更多资源。我正在使用 Python 2.7.6。谢谢

measure = raw_input("How would you like to measure the coins? Enter 1 for grams 2 for pounds.  ")

coin_args = [
["pennies", '2.5', '50.0', '.01'] 
["nickles", '5.0', '40.0', '.05']
["dimes", '2.268', '50.0', '.1']
["quarters", '5.67', '40.0', '.25']
]

if measure == 2:
    for coin, coin_weight, rolls, worth in coin_args:
        print "Enter the weight of your %s" % (coin)
        weight = float(raw_input())
        convert2grams = weight * 453.592

        num_coin = convert2grams / (float(coin_weight))
        num_roll = round(num_coin / (float(rolls)))
        amount = round(num_coin * (float(worth)), 2)

        print "You have %d %s, worth $ %d, and will need %d rolls." % (num_coin, coin, amount, num_roll)

else:
    for coin, coin_weight, rolls, worth in coin_args:
        print "Enter the weight of your %s" % (coin)
        weight = float(raw_input())

        num_coin = weight / (float(coin_weight))
        num_roll = round(num_coin / (float(rolls)))
        amount = round(num_coin * (float(worth)), 2)

        print "You have %d %s, worth $ %d, and will need %d rolls." % (num_coin, coin, amount, num_roll)

这是堆栈跟踪:

File ".\coin_estimator_by_weight.py", line 5, in <module>
  ["nickles", '5.0', '40.0', '.05']
TypeError: list indices must be integers, not tuple

【问题讨论】:

  • 您不会解析来自raw_input 的结果。它永远不会是2
  • 由于您两次编写或多或少相同的源代码,您应该考虑如何使这更优雅。其实你只需要区分重量单位,根据需要转换输入,其余的都一样。
  • OBu 一个很好的观点。我将努力改进我的代码。我假设您正在谈论制作功能。

标签: python list


【解决方案1】:

问题是python中的[...]有两个不同的含义

  1. expr [ index ] 表示访问列表的元素
  2. [ expr1, expr2, expr3 ] 表示从三个表达式构建一个包含三个元素的列表

在您的代码中,您忘记了外部列表中项目的表达式之间的逗号:

[ [a, b, c] [d, e, f] [g, h, i] ]

因此,Python 将第二个元素的开头解释为要应用于第一个元素的索引,这就是错误消息的含义。

您正在寻找的正确语法是

[ [a, b, c], [d, e, f], [g, h, i] ]

【讨论】:

    【解决方案2】:

    要创建列表列表,你需要用逗号分隔它们,像这样

    coin_args = [
        ["pennies", '2.5', '50.0', '.01'],
        ["nickles", '5.0', '40.0', '.05'],
        ["dimes", '2.268', '50.0', '.1'],
        ["quarters", '5.67', '40.0', '.25']
    ]
    

    【讨论】:

      【解决方案3】:

      为什么错误会提到元组?

      其他人已经解释说问题是缺少,,但最后的谜团是为什么错误消息会谈论元组?

      作为mentioned by 6502代码:

      coin_args = [
        ["pennies", '2.5', '50.0', '.01'] 
        ["nickles", '5.0', '40.0', '.05']
      ]
      

      有完全相同的问题:

      coin_args = [
        ["pennies", '2.5', '50.0', '.01']["nickles", '5.0', '40.0', '.05']
      ]
      

      与以下问题相同:

      mylist = ["pennies", '2.5', '50.0', '.01']
      coin_args = [
        mylist["nickles", '5.0', '40.0', '.05']
      ]
      

      与以下问题相同:

      mylist = [1, 2]
      print(mylist[3, 4])
      

      当您执行mylist[0] 时,它调用__getitem__,它处理[] 分辨率。

      但 Python 语法通常也允许您传递两个参数,例如:object[1, 2]。发生这种情况时,__getitem__ 会收到一个元组:

      class C(object):
          def __getitem__(self, k):
              return k
      
      # Single argument is passed directly.
      assert C()[0] == 0
      
      # Multiple indices generate a tuple.
      assert C()[0, 1] == (0, 1)
      

      问题是list 内置类的__getitem__ 不能处理这样的元组参数,只能处理整数,所以抱怨:

      TypeError: list indices must be integers, not tuple
      

      但是,您可以在自己的类中实现 __getitem__,以便 myobject[1, 2] 做一些明智的事情。

      __getitem__ 操作的更多示例:https://stackoverflow.com/a/33086813/895245

      【讨论】:

      • 起初我并不清楚措辞。经过测试,我了解到:“可以简化为:[][1, 2]”意味着被二维切片的一维列表(甚至是空列表)([1,2][:1] 也可以)抛出相同的错误TypeError: list indices must be integers, not tuple。从那里开始,答案将 [] 称为列表,将 [1, 2] 称为切片器。
      【解决方案4】:

      对我来说这是可行的

      input = [image2[tf.newaxis, ...], image1[tf.newaxis, ...], imagenew[tf.newaxis, ...]]
      

      所以检查 , 和 []

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-19
        • 1970-01-01
        • 2015-07-11
        • 1970-01-01
        • 2012-03-11
        • 1970-01-01
        • 2017-02-11
        • 1970-01-01
        相关资源
        最近更新 更多