python - What's going on here? Repeating rows in random list of lists -


I want to get a grid of unique random numbers. Instead, each line number has the same order. What is happening?

Border in C (NCOL): Grid [R] Examples output: [64, 82, 90, 69] [c] = int (random (* * 100)) pippin (grid)

36], [64, 82, 90, 69, 36], [64, 82, 90, 69, 36], [64, 82, 90, 69, 36], [64, 82, 90, 69, 36] ]

I think this is because the dragon uses weak copy in the list

  grid = [...] * nrows  

I tried hard to list and it worked correctly:

  & gt; & Gt; & Gt; Grid = [[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0] , [0,0,0,0,0]]] gt; & Gt; & Gt; For R in Category (NRO): ... border in NC (NCOL): ... grid [r] [c] = int (random (* * 100 * ... ... ... gt; & gt; ; [36, 21, 41, 29, [65, 67, 88, 77, 76] [65, 67, 40, 41, 50, 92] [26, 42, 64, 77, 77], [65, 67, 88, 77, 76] 25], [98, 77, 38, 40, 96]]  

It tells me that when Python copies the list 5 times Is doing all this, collecting 5 points in your first list - so, when you change the values ​​in that list, then you are actually changing the values ​​in the first list and copy it in all the lists. Which indicates that one.

By using your method, you can use '

  grid = [[0] * NCOL for the rows: / P> 

Instead, I suggest changing my list generation line to look more like this: Range (nkol)]

5 separate lists should be created for.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -