I have two lists a b
with a random number of elements (let's say 8) which I want to split in the point cp
. After that, I want to keep the part of the list before cp
as it was and change the part after cp
for its part in the other list.
I use the following code:
cp = 4
a = [1, 3, 2, 4, 5, 8, 7, 6]
b = [3, 1, 5, 6, 2, 8, 4, 7]
parents = np.array([a,b])
parents[0][cp:], parents[1][cp:] = parents[1][cp:], parents[0][cp:]
print parents
# Print result:
# [[1 3 2 4 5 8 7 6]
# [3 1 5 6 5 8 7 6]]
As show in the code, I am getting an error apparently because it assigns the values of the sub-array in parents[0]
before the assignation has ended.
If I use the traditional python lists this seems to work fine:
a = [1,3,2,4, 5,8,7,6]
b = [3,1,5,6, 2,8,4,7]
a[cp:] , b[cp:] = b[cp:], a[cp:]
print a,b
# Print result:
# [1, 3, 2, 4, 2, 8, 4, 7] [3, 1, 5, 6, 5, 8, 7, 6]
Is there a way to make this work using the previous notation and not adding a third var?