Novice with programming in general here. I have an empty dictionary with no number of keys, (where no in this case = 3). I'm trying to generate a list of lists that will be added to the dictionary within a loop, but over each loop the other keys are being overwritten. See my attempt below:
#generating dictionary for all fibres with number n
fibre = {i : {}for i in range(no)}
#generating empty fibre of size n_f
fibre_e = [[[] for i in range(n_f)] for j in range(n_f)]
rand_r = [[] for a in range(no)]
rand_c = [[] for a in range(no)]
#generating fibre element from random corner elements
for a in range(no):
# choosing a random corner element
fibre[a] = fibre_e
rand_r[a] = randrange(len(all_e))
rand_c[a] = randrange(len(all_e))
for i in range(n_f):
e1_fibre=all_e[rand_r[a]][rand_c[a]]
#move to upper row
rand_r[a] += 1
for j in range(n_f):
fibre_e[i][j] = e1_fibre
e1_fibre += 1
print(fibre)
This is the output when I checked the progression:
{0: [[57, 58], [66, 67]], 1: {}, 2: {}}
{0: [[33, 34], [42, 43]], 1: [[33, 34], [42, 43]], 2: {}}
{0: [[29, 30], [38, 39]], 1: [[29, 30], [38, 39]], 2: [[29, 30], [38, 39]]}
For context, this is to generate an abaqus mesh file with a random distribution of identical fibre elements within a matrix.
edit:context
Using copy.deepcopy worked
for a in range(no):
# choosing a random corner element
rand_r[a] = randrange(len(all_e))
rand_c[a] = randrange(len(all_e))
for i in range(n_f):
e1_fibre=all_e[rand_r[a]][rand_c[a]]
#move to upper row
rand_r[a] += 1
for j in range(n_f):
fibre_e[i][j] = e1_fibre
e1_fibre += 1
fibre[a] = copy.deepcopy(fibre_e)
print(fibre)
Output:
{0: [[68, 69], [77, 78]], 1: {}, 2: {}}
{0: [[68, 69], [77, 78]], 1: [[6, 7], [15, 16]], 2: {}}
{0: [[68, 69], [77, 78]], 1: [[6, 7], [15, 16]], 2: [[28, 29], [37, 38]]}