38

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

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
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Aaron
  • 491
  • 1
  • 4
  • 4
  • 2
    You don't parse the result from `raw_input`. It will never be `2`. – Fred Foo Feb 09 '14 at 17:42
  • 1
    Since you wrote more or less the same source code twice, you should think of how you can make this more elegant. In fact, you onyl have to distinguish the weight unit, convert the input if needed, and the rest is the same. – OBu Feb 09 '14 at 17:49
  • OBu a very good point. I will work on refining my code. I am assuming you are talking about making a function. – Aaron Feb 09 '14 at 18:15

4 Answers4

76

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

  1. expr [ index ] means accessing an element of a list
  2. [ expr1, expr2, expr3 ] means building a list of three elements from three expressions

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.

The correct syntax for what you're looking for is

[ [a, b, c], [d, e, f], [g, h, i] ]
6502
  • 112,025
  • 15
  • 165
  • 265
14

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']
]
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
11

Why does the error mention tuples?

Others have explained that the problem was the missing ,, but the final mystery is why does the error message talk about tuples?

As mentioned by 6502 the code:

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

has the exact same problem as:

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

which has the same problem as:

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

which has the same problem as:

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

When you do mylist[0], that calls __getitem__, which deals with [] resolution.

But Python syntax also allows you to pass two arguments in general, e.g.: object[1, 2]. When that happens, __getitem__ receives a tuple:

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)

The problem is that the __getitem__ for the list built-in class cannot deal with tuple arguments like that, only integers, and so in complains:

TypeError: list indices must be integers, not tuple

You could however implement __getitem__ in your own classes such that myobject[1, 2] does something sensible.

More examples of __getitem__ action at: https://stackoverflow.com/a/33086813/895245

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 1
    The wording was not clear to me at first. After testing around, I understood: "can be reduced to: `[][1, 2]`" means that a one-dimensional list (and even an empty list) that is sliced by two dimensions (`[1,2][:1]` would work as well) throws the same error `TypeError: list indices must be integers, not tuple`. And from there, the answer refers to [] as the list and [1, 2] as the slicer. – questionto42 Sep 15 '21 at 12:28
0

For me this works

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

So check for , and []

Andronicus
  • 25,419
  • 17
  • 47
  • 88
VVK
  • 1