Python, two dimensional list and coordinates

拜拜、爱过 提交于 2019-12-02 07:35:33

Just assign directly to those two index pairs, indexing from the outer list to the inner (the last list is 2, the middle list is 1), so the first element of the last list is at [2][0]:

outerlist[1][0], outerlist[2][0] = outerlist[2][0], 0

This assigns two values (one taken from outerlist[0][2], the other the literal 0 integer) to the two positions in the nested lists.

If you wanted to swap those two positions (taking the 0 from outerlist[0][1]), then do so with the same syntax:

outerlist[1][0], outerlist[2][0] = outerlist[2][0], outerlist[1][0]

because the right-hand side expression is evaluated before assigning the two values to the left-hand side targets:

>>> outerlist = [[1, 2, 3, 4, 5], [0, 0, 0, 0, 0], [6, 7, 8, 9, 10]]
>>> outerlist
[[1, 2, 3, 4, 5], [0, 0, 0, 0, 0], [6, 7, 8, 9, 10]]
>>> outerlist[1][0], outerlist[2][0] = outerlist[2][0], outerlist[1][0]
>>> outerlist
[[1, 2, 3, 4, 5], [6, 0, 0, 0, 0], [0, 7, 8, 9, 10]]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!