Python,二维列表和坐标 [英] Python, two dimensional list and coordinates
问题描述
我有一个二维列表:
def list():
list1 =[1,2,3,4,5]
list2 =[0,0,0,0,0]
list3 =[6,7,8,9,10]
list=[list1,list2,list3]
for i in list:
print(i)
list()
6将具有坐标(0,2),对吗? 我想将6移到(0,1),当我这样做时,我也想将(0,2)变成0.
6 will have the coordinates (0,2), right?? I want to move 6 to (0,1) and when I do that, I also want (0,2) to become 0.
我该怎么做?我不知道..我是一个初学者.
How do I do that? I have no idea.. I'm a beginner at this.
推荐答案
只需直接分配给这两个索引对,即从外部列表到内部索引(最后一个列表是2
,中间列表是1
),因此最后一个列表的第一个元素位于[2][0]
:
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
这会将两个值(一个取自outerlist[0][2]
,另一个取自文本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.
如果您想交换这两个位置(从outerlist[0][1]
中获取0
),请使用相同的语法:
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]]
这篇关于Python,二维列表和坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!