vendredi 12 novembre 2021

How can I point to only one element in a 2d list? [duplicate]

I don't know why the whole column is changed for the code below.

from random import *
map=[]
lst=[]
for i in range(0,10):
    lst.append(".")
for j in range(0,10):
    map.append(lst)

col=randint(0,9)
row=randint(0,9)

if map[row][col]==".":
    map[row][col]="ex"

print(map)

Output

[
  ['.', '.', '.', '.', 'ex', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', 'ex', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', 'ex', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', 'ex', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', 'ex', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', 'ex', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', 'ex', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', 'ex', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', 'ex', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', 'ex', '.', '.', '.', '.', '.']
]

Expected

[
  ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', 'ex', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], 
  ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.']
]



Aucun commentaire:

Enregistrer un commentaire