I need to create a function that swaps specific elements in a nested list. This is the code im working with, and the list is dg.
events_b_list = []
dg = [[[a1,b1], [a1,b1]],[[a2,b2], [a2,b2]], [[a3,b3], [a3,b3]],[[a4,b4], [a4,b4]]]
for recomb_r1 in [0,1]:
if (recomb_r1 == 0):
p_1 = (1-r)
dg_0 = dg
else:
p_1 = r
for i in dg swap_r()
dg.append()
for recomb_r2 in [0,1]:
if (recomb_r2 == 0):
p_2 = (1-r)
dg_0 = dg
else:
p_2 = r
for i in dg swap_r()
dg.append()
recomb_r1 refers to a swap between the first b1 and the first b3. [[[a1,b1], [a1,b1]],[[a2,b2], [a2,b2]], [[a3,b3], [a3,b3]],[[a4,b4], [a4,b4]]]. Basically, if recomb_r1 ==0 the current list in dg dosn't change, but if recomb_r1 does happen then a new, changed list is added into dg with the changed elements. So the new dg would look like this,
dg= [[[a1,b1], [a1,b1]],[[a2,b2], [a2,b2]], [[a3,b3], [a3,b3]],[[a4,b4], [a4,b4]], [[a1,b3], [a1,b1]],[[a2,b2], [a2,b2]], [[a3,b1], [a3,b3]],[[a4,b4], [a4,b4]]]
Similarly, the next step is another swap but between the second b1 and second b4. If the swap doesn't happen the latest list remains the same and if it does the list should make the swap in for elements in the list and form a new list in dg (consisting of the changes). I'm wondering if there is a function that can do the switches and iterate thru dg to create a new list of the changes?
You can swap the elements by making a copy of the original one and then, simply concatenate them.
import copy
events_b_list = []
dg = [[[a1,b1], [a1,b1]],[[a2,b2], [a2,b2]], [[a3,b3], [a3,b3]],[[a4,b4], [a4,b4]]]
for recomb_r1 in [0,1]:
if (recomb_r1 == 0):
p_1 = (1-r)
dg_0 = dg
else:
p_1 = r
fg = copy.deepcopy(dg)
fg[0][0][1], fg[2][0][1] = fg[2][0][1], fg[0][0][1]
dg = dg + fg