Python 'list indices must be integers, not tuple"

前端 未结 3 2028
南旧
南旧 2021-02-01 02:21

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 readin

3条回答
  •  误落风尘
    2021-02-01 02:56

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

提交回复
热议问题