vendredi 25 décembre 2020

How do I change a specific vertex in a matrix list in python [duplicate]

import random

rows, cols = (24, 24) 
node = [[0]*cols]*rows 

def main():
    for i in range(23):
        while True:
            randx = random.randint(0,23)
            randy = random.randint(0,23)
            if node[randx][randy] == 0:
                node[randx][randy] = 1
                break
    print(node)
main()

For some reason it returns a matrix with all of the rows looking the same. for example it might produce:

[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0], 
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0], 
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0], 
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0], 
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0], ...

the whole matrix would look like that. (I have just taken the first 5 rows to demonstrate) I am trying to make it change individual vertices in the matrix, not entire rows.




Aucun commentaire:

Enregistrer un commentaire