Python“列表索引必须是整数,而不是元组”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21662532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:21:27  来源:igfitidea点击:

Python 'list indices must be integers, not tuple"

pythonlist

提问by Aaron

I have been banging my head against this for two days now. I am new to python and programming so the other examples of this type of error have not helped me to much. I am reading through the documentation for lists and tuples, but haven't found anything that helps. Any pointer would be much appreciated. Not looking for the answer necessarily, just more resources on where to look. I am using Python 2.7.6. Thanks

这两天我一直在反对这个。我是 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)

This is the stack trace:

这是堆栈跟踪:

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

采纳答案by 6502

The problem is that [...]in python has two distinct meanings

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

  1. expr [ index ]means accessing an element of a list
  2. [ expr1, expr2, expr3 ]means building a list of three elements from three expressions
  1. expr [ index ]表示访问列表的元素
  2. [ expr1, expr2, expr3 ]意味着从三个表达式构建一个包含三个元素的列表

In your code you forgot the comma between the expressions for the items in the outer list:

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

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

therefore Python interpreted the start of second element as an index to be applied to the first and this is what the error message is saying.

因此 Python 将第二个元素的开始解释为要应用于第一个元素的索引,这就是错误消息所说的内容。

The correct syntax for what you're looking for is

您正在寻找的正确语法是

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

回答by thefourtheye

To create list of lists, you need to separate them with commas, like this

要创建列表列表,您需要用逗号分隔它们,如下所示

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']
]