we have an array p = [[none]*5]*6 and we want to fill it with number 1
something like this:
[[1, none, none, 1, 1],[none, 1, none, 1, none],[none, 1, 1, 1, none], [1, none, 1, none, 1], [none, 1, none, 1, none],[1, none, 1, none, none]]
we have an array p = [[none]*5]*6 and we want to fill it with number 1
something like this:
[[1, none, none, 1, 1],[none, 1, none, 1, none],[none, 1, 1, 1, none], [1, none, 1, none, 1], [none, 1, none, 1, none],[1, none, 1, none, none]]
Tt isn't a good idea to use p = [[none]*5]*6
because with this expression every list in p is linked together, which causes some weird behaviours like for example:
>>> p = [[None]*5]*6
>>> p
[[None, None, None, None, None], [None, None, None, None, None], [None, None, None, None, None], [None, None, None, None, None], [None, None, None, None, None], [None, None, None, None, None]]
>>> p[0][0] = 1
[[1, None, None, None, None], [1, None, None, None, None], [1, None, None, None, None], [1, None, None, None, None], [1, None, None, None, None], [1, None, None, None, None]]
To create your array you can use a list comprehension:
p = [[none]*5 for _ in range(6)]
and to randomise it you can create the function:
from random import random
def randomise_array(array):
for index1, list1 in enumerate(array):
for index2 in range(len(list1)):
if random()>0.5:
array[index1][index2]=1
return array
PS: None
takes a capital "N"