问题更改列表列表中的单个项目 - Python
Issue changing single item in a list of lists - Python
好的。所以我正在学习编程基础知识,对于我们的最终项目,我们必须设计一个带有地图的游戏。我创建了一个程序,它创建了一个包含十个列表的列表,每个列表有十个项目。但是,当我尝试更改一个列表中的单个项目时(例如 mapGrid[4][5] = "i"
),它会更改所有列表中的所有第 6 个项目。
这是我用来测试它的代码:
import random
def createMap():
mapGrid = []
col = []
randx = 0
randy = 0
i = 0
for x in range(0,10):
col += "#"
for y in range(0,10):
mapGrid.append(col)
while i < 10:
randx = random.randint(0,9)
randy = random.randint(0,9)
if mapGrid[randx][randy] != "i":
mapGrid[randx][randy] = "i"
i += 1
return mapGrid
def printMap(x,y,mapGrid):
mapGrid[x][y] = "0"
print("",mapGrid[x-1][y+1],mapGrid[x][y+1],mapGrid[x+1][y+1],"\n",
mapGrid[x-1][y],mapGrid[x][y],mapGrid[x+1][y],"\n",
mapGrid[x-1][y-1],mapGrid[x][y-1],mapGrid[x+1][y-1])
examp = createMap()
print(examp)
print("")
printMap(4,4,examp)
我不断得到的结果是:
[['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i']]
i i i
0 0 0
i i i
不再有十个单独的 i 和一个“0”,而是全部为 i,每个列表中的每第 5 个项目都是 0。
我该如何解决才能只更改一个项目而不是每个列表中的一个项目?
您多次附加相同的 col
列表,您想要的是每次都创建一个新副本:
mapGrid.append(col[:]) # a copy of col
您向地图网格添加了 10 次相同的列表 col
,每次都不是副本。因此,while
循环中的每个更改都会以相同的方式影响 mapGrid 中的每个 column/sublist - 而不仅仅是其中一个。
改变
for y in range(0,10):
mapGrid.append(col)
到
for y in range(0,10):
mapGrid.append(list(col))
产出
[['i', '#', '#', '#', '#', '#', '#', '#', '#', '#'], ['#', '#', '#', '#', 'i', '
#', 'i', '#', '#', '#'], ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#'], ['#
', '#', '#', '#', '#', '#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#', '#',
'i', '#', '#', '#'], ['#', '#', '#', '#', 'i', '#', '#', 'i', '#', '#'], ['#', '
#', '#', '#', '#', 'i', '#', '#', '#', '#'], ['#', '#', 'i', '#', '#', '#', '#',
'#', '#', '#'], ['#', 'i', '#', '#', '#', '#', '#', '#', '#', '#'], ['#', '#',
'#', '#', '#', 'i', '#', '#', '#', '#']]
('', '#', '#', '#', '\n', '#', '0', 'i', '\n', '#', '#', '#')
问题是您创建了一个列表,其中有 10 个 references 到同一个列表,在这个列表中:
for y in range(0,10):
mapGrid.append(col)
您每次都需要构建一个 new 列表 - 您可以按照 this FAQ entry 使用 col[:]
或构建一个新列表来完成此操作明确地用 list(col)
.
好的。所以我正在学习编程基础知识,对于我们的最终项目,我们必须设计一个带有地图的游戏。我创建了一个程序,它创建了一个包含十个列表的列表,每个列表有十个项目。但是,当我尝试更改一个列表中的单个项目时(例如 mapGrid[4][5] = "i"
),它会更改所有列表中的所有第 6 个项目。
这是我用来测试它的代码:
import random
def createMap():
mapGrid = []
col = []
randx = 0
randy = 0
i = 0
for x in range(0,10):
col += "#"
for y in range(0,10):
mapGrid.append(col)
while i < 10:
randx = random.randint(0,9)
randy = random.randint(0,9)
if mapGrid[randx][randy] != "i":
mapGrid[randx][randy] = "i"
i += 1
return mapGrid
def printMap(x,y,mapGrid):
mapGrid[x][y] = "0"
print("",mapGrid[x-1][y+1],mapGrid[x][y+1],mapGrid[x+1][y+1],"\n",
mapGrid[x-1][y],mapGrid[x][y],mapGrid[x+1][y],"\n",
mapGrid[x-1][y-1],mapGrid[x][y-1],mapGrid[x+1][y-1])
examp = createMap()
print(examp)
print("")
printMap(4,4,examp)
我不断得到的结果是:
[['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'], ['i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i']]
i i i
0 0 0
i i i
不再有十个单独的 i 和一个“0”,而是全部为 i,每个列表中的每第 5 个项目都是 0。
我该如何解决才能只更改一个项目而不是每个列表中的一个项目?
您多次附加相同的 col
列表,您想要的是每次都创建一个新副本:
mapGrid.append(col[:]) # a copy of col
您向地图网格添加了 10 次相同的列表 col
,每次都不是副本。因此,while
循环中的每个更改都会以相同的方式影响 mapGrid 中的每个 column/sublist - 而不仅仅是其中一个。
改变
for y in range(0,10):
mapGrid.append(col)
到
for y in range(0,10):
mapGrid.append(list(col))
产出
[['i', '#', '#', '#', '#', '#', '#', '#', '#', '#'], ['#', '#', '#', '#', 'i', '
#', 'i', '#', '#', '#'], ['#', '#', '#', '#', '#', '#', '#', '#', '#', '#'], ['#
', '#', '#', '#', '#', '#', '#', '#', '#', '#'], ['#', '#', '#', '#', '#', '#',
'i', '#', '#', '#'], ['#', '#', '#', '#', 'i', '#', '#', 'i', '#', '#'], ['#', '
#', '#', '#', '#', 'i', '#', '#', '#', '#'], ['#', '#', 'i', '#', '#', '#', '#',
'#', '#', '#'], ['#', 'i', '#', '#', '#', '#', '#', '#', '#', '#'], ['#', '#',
'#', '#', '#', 'i', '#', '#', '#', '#']]
('', '#', '#', '#', '\n', '#', '0', 'i', '\n', '#', '#', '#')
问题是您创建了一个列表,其中有 10 个 references 到同一个列表,在这个列表中:
for y in range(0,10):
mapGrid.append(col)
您每次都需要构建一个 new 列表 - 您可以按照 this FAQ entry 使用 col[:]
或构建一个新列表来完成此操作明确地用 list(col)
.