繁体   English   中英

如何修复列表错误中的元组

[英]How do I fix a tuple in list error

所以我有这段代码:

Points = [ [400,100],[600,100],[800,100] , [300,300],[400,300],[500,300],[600,300] ,         [200,500],[400,500],[600,500],[800,500],[1000,500] , [300,700],[500,700][700,700][900,700] , [200,900],[400,900],[600,900] ]

并产生此错误:

  line 43, in <module>
    Points = [ [400,100],[600,100],[800,100] , [300,300],[400,300],[500,300],[600,300] , [200,500],[400,500],[600,500],[800,500],[1000,500] , [300,700],[500,700][700,700][900,700] , [200,900],[400,900],[600,900] ]
TypeError: list indices must be integers, not tuple

我该如何解决?

您忘记了两个逗号:

[500,700][700,700][900,700]

现在,Python看到尝试用(700, 700)元组在左侧索引列表的尝试:

>>> [500,700][700,700]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not tuple

第二个[900, 700] “列表”会给您同样的问题,但尚未发挥作用。

通过在以下之间添加逗号来解决此问题:

[500, 700], [700, 700], [900, 700]

或者,作为完整列表:

 Points = [[400, 100], [600, 100], [800, 100], [300, 300], [400, 300], [500, 300], [600, 300], [200, 500], [400, 500], [600, 500], [800, 500], [1000, 500], [300, 700], [500, 700], [700, 700], [900, 700], [200, 900], [400, 900], [600, 900]]

您忘了用逗号分隔一些。 请参阅修复程序。

>>> Points = [[400,100], [600,100], [800,100], [300,300], [400,300], [500,300], [600,300] ,[200,500], [400,500], [600,500], [800,500], [1000,500], [300,700], [500,700], [700,700],[900,700], [200,900], [400,900], [600,900]]

忘记逗号会使Python相信您正在尝试访问第一个列表和第二个列表,这会引发错误。

你需要每个表的一个单独的(外清单) ,

Points = [ [400,100],[600,100],[800,100] , [300,300],[400,300],[500,300],[600,300] ,[200,500],[400,500],[600,500],[800,500],[1000,500] , [300,700],[500,700],[700,700],[900,700] , [200,900],[400,900],[600,900] ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM