Skip to content
Advertisement

Modifying an item in a list of lists, changes also other lists same item position

The following code is part of a function but the problem I have is on these lines.

# Global variables
NUM_MECS          = 2
MAX_MEC_CAPACITY  = 2
VNF_TYPES         = 2

# Mec capacity to hold a vnf
served_vnfs = list(range(MAX_MEC_CAPACITY+1)) # 0 does not  count as capacity
# All possible mec states as far as holded vnfs
mec_capacity_states = [list(s) for s in itertools.product(served_vnfs, repeat=VNF_TYPES)]
# All possible states with defined number of mecs
mecs_states = [list(p) for p in itertools.product(mec_capacity_states, repeat=NUM_MECS)]

and this generates a list of lists as:

out: [[[0, 0], [0, 0]], [[0, 0], [0, 1]], [[0, 0], [0, 2]], [[0, 0], [1, 0]], [[0, 0], [1, 1]], [[0, 0], [1, 2]], [[0, 0], [2, 0]], [[0, 0], [2, 1]], [[0, 0], [2, 2]], [[0, 1], [0, 0]], [[0, 1], [0, 1]], [[0, 1], [0, 2]], [[0, 1], [1, 0]], [[0, 1], [1, 1]], [[0, 1], [1, 2]], [[0, 1], [2, 0]], [[0, 1], [2, 1]], [[0, 1], [2, 2]], [[0, 2], [0, 0]], [[0, 2], [0, 1]], [[0, 2], [0, 2]], [[0, 2], [1, 0]], [[0, 2], [1, 1]], [[0, 2], [1, 2]], [[0, 2], [2, 0]], [[0, 2], [2, 1]], [[0, 2], [2, 2]], [[1, 0], [0, 0]], [[1, 0], [0, 1]], [[1, 0], [0, 2]], [[1, 0], [1, 0]], [[1, 0], [1, 1]], [[1, 0], [1, 2]], [[1, 0], [2, 0]], [[1, 0], [2, 1]], [[1, 0], [2, 2]], [[1, 1], [0, 0]], [[1, 1], [0, 1]], [[1, 1], [0, 2]], [[1, 1], [1, 0]], [[1, 1], [1, 1]], [[1, 1], [1, 2]], [[1, 1], [2, 0]], [[1, 1], [2, 1]], [[1, 1], [2, 2]], [[1, 2], [0, 0]], [[1, 2], [0, 1]], [[1, 2], [0, 2]], [[1, 2], [1, 0]], [[1, 2], [1, 1]], [[1, 2], [1, 2]], [[1, 2], [2, 0]], [[1, 2], [2, 1]], [[1, 2], [2, 2]], [[2, 0], [0, 0]], [[2, 0], [0, 1]], [[2, 0], [0, 2]], [[2, 0], [1, 0]], [[2, 0], [1, 1]], [[2, 0], [1, 2]], [[2, 0], [2, 0]], [[2, 0], [2, 1]], [[2, 0], [2, 2]], [[2, 1], [0, 0]], [[2, 1], [0, 1]], [[2, 1], [0, 2]], [[2, 1], [1, 0]], [[2, 1], [1, 1]], [[2, 1], [1, 2]], [[2, 1], [2, 0]], [[2, 1], [2, 1]], [[2, 1], [2, 2]], [[2, 2], [0, 0]], [[2, 2], [0, 1]], [[2, 2], [0, 2]], [[2, 2], [1, 0]], [[2, 2], [1, 1]], [[2, 2], [1, 2]], [[2, 2], [2, 0]], [[2, 2], [2, 1]], [[2, 2], [2, 2]]]

If for example I want to modify only the second value in the second item from the second list:

print(mecs_states[1], '-', mecs_states[1][1], '-', mecs_states[1][1][1])

mecs_states[1][1][1] = 'A'

Is not only changing the desired value but also other lists values in the same index position.

out: [[[0, 0], [0, 0]], [[0, 0], [0, 'A']], [[0, 0], [0, 2]], [[0, 0], [1, 0]], [[0, 0], [1, 1]], [[0, 0], [1, 2]], [[0, 0], [2, 0]], [[0, 0], [2, 1]], [[0, 0], [2, 2]], [[0, 'A'], [0, 0]], [[0, 'A'], [0, 'A']], [[0, 'A'], [0, 2]], [[0, 'A'], [1, 0]], [[0, 'A'], [1, 1]], [[0, 'A'], [1, 2]], [[0, 'A'], [2, 0]], [[0, 'A'], [2, 1]], [[0, 'A'], [2, 2]], [[0, 2], [0, 0]], [[0, 2], [0, 'A']], [[0, 2], [0, 2]], [[0, 2], [1, 0]], [[0, 2], [1, 1]], [[0, 2], [1, 2]], [[0, 2], [2, 0]], [[0, 2], [2, 1]], [[0, 2], [2, 2]], [[1, 0], [0, 0]], [[1, 0], [0, 'A']], [[1, 0], [0, 2]], [[1, 0], [1, 0]], [[1, 0], [1, 1]], [[1, 0], [1, 2]], [[1, 0], [2, 0]], [[1, 0], [2, 1]], [[1, 0], [2, 2]], [[1, 1], [0, 0]], [[1, 1], [0, 'A']], [[1, 1], [0, 2]], [[1, 1], [1, 0]], [[1, 1], [1, 1]], [[1, 1], [1, 2]], [[1, 1], [2, 0]], [[1, 1], [2, 1]], [[1, 1], [2, 2]], [[1, 2], [0, 0]], [[1, 2], [0, 'A']], [[1, 2], [0, 2]], [[1, 2], [1, 0]], [[1, 2], [1, 1]], [[1, 2], [1, 2]], [[1, 2], [2, 0]], [[1, 2], [2, 1]], [[1, 2], [2, 2]], [[2, 0], [0, 0]], [[2, 0], [0, 'A']], [[2, 0], [0, 2]], [[2, 0], [1, 0]], [[2, 0], [1, 1]], [[2, 0], [1, 2]], [[2, 0], [2, 0]], [[2, 0], [2, 1]], [[2, 0], [2, 2]], [[2, 1], [0, 0]], [[2, 1], [0, 'A']], [[2, 1], [0, 2]], [[2, 1], [1, 0]], [[2, 1], [1, 1]], [[2, 1], [1, 2]], [[2, 1], [2, 0]], [[2, 1], [2, 1]], [[2, 1], [2, 2]], [[2, 2], [0, 0]], [[2, 2], [0, 'A']], [[2, 2], [0, 2]], [[2, 2], [1, 0]], [[2, 2], [1, 1]], [[2, 2], [1, 2]], [[2, 2], [2, 0]], [[2, 2], [2, 1]], [[2, 2], [2, 2]]]

What am I missing? I need that each list within a list be independent. I’m guessing that the reason is in the product of itertools but I don’t totally understand why.

What would be the solution?

Advertisement

Answer

Using json to serialize and deserialize.

import json

# Mec capacity to hold a vnf
served_vnfs = list(range(MAX_MEC_CAPACITY + 1))  # 0 does not  count as capacity
# All possible mec states as far as holded vnfs
mec_capacity_states = [list(s) for s in itertools.product(served_vnfs, repeat=VNF_TYPES)]
# All possible states with defined number of mecs
mecs_states = [copy.deepcopy(list(p)) for p in 
itertools.product(mec_capacity_states, repeat=NUM_MECS)]
mecs_states = json.loads(json.dumps(list(mecs_states)))

print(mecs_states)
mecs_states[0][0][0] = 'A'
print(mecs_states)

output

[[[0, 0], [0, 0]], [[0, 0], [0, 1]], [[0, 0], [0, 2]], [[0, 0], [1, 0]], [[0, 0], [1, 1]], [[0, 0], [1, 2]], [[0, 0], [2, 0]], [[0, 0], [2, 1]], [[0, 0], [2, 2]], [[0, 1], [0, 0]], [[0, 1], [0, 1]], [[0, 1], [0, 2]], [[0, 1], [1, 0]], [[0, 1], [1, 1]], [[0, 1], [1, 2]], [[0, 1], [2, 0]], [[0, 1], [2, 1]], [[0, 1], [2, 2]], [[0, 2], [0, 0]], [[0, 2], [0, 1]], [[0, 2], [0, 2]], [[0, 2], [1, 0]], [[0, 2], [1, 1]], [[0, 2], [1, 2]], [[0, 2], [2, 0]], [[0, 2], [2, 1]], [[0, 2], [2, 2]], [[1, 0], [0, 0]], [[1, 0], [0, 1]], [[1, 0], [0, 2]], [[1, 0], [1, 0]], [[1, 0], [1, 1]], [[1, 0], [1, 2]], [[1, 0], [2, 0]], [[1, 0], [2, 1]], [[1, 0], [2, 2]], [[1, 1], [0, 0]], [[1, 1], [0, 1]], [[1, 1], [0, 2]], [[1, 1], [1, 0]], [[1, 1], [1, 1]], [[1, 1], [1, 2]], [[1, 1], [2, 0]], [[1, 1], [2, 1]], [[1, 1], [2, 2]], [[1, 2], [0, 0]], [[1, 2], [0, 1]], [[1, 2], [0, 2]], [[1, 2], [1, 0]], [[1, 2], [1, 1]], [[1, 2], [1, 2]], [[1, 2], [2, 0]], [[1, 2], [2, 1]], [[1, 2], [2, 2]], [[2, 0], [0, 0]], [[2, 0], [0, 1]], [[2, 0], [0, 2]], [[2, 0], [1, 0]], [[2, 0], [1, 1]], [[2, 0], [1, 2]], [[2, 0], [2, 0]], [[2, 0], [2, 1]], [[2, 0], [2, 2]], [[2, 1], [0, 0]], [[2, 1], [0, 1]], [[2, 1], [0, 2]], [[2, 1], [1, 0]], [[2, 1], [1, 1]], [[2, 1], [1, 2]], [[2, 1], [2, 0]], [[2, 1], [2, 1]], [[2, 1], [2, 2]], [[2, 2], [0, 0]], [[2, 2], [0, 1]], [[2, 2], [0, 2]], [[2, 2], [1, 0]], [[2, 2], [1, 1]], [[2, 2], [1, 2]], [[2, 2], [2, 0]], [[2, 2], [2, 1]], [[2, 2], [2, 2]]]

[[['A', 0], [0, 0]], [[0, 0], [0, 1]], [[0, 0], [0, 2]], [[0, 0], [1, 0]], [[0, 0], [1, 1]], [[0, 0], [1, 2]], [[0, 0], [2, 0]], [[0, 0], [2, 1]], [[0, 0], [2, 2]], [[0, 1], [0, 0]], [[0, 1], [0, 1]], [[0, 1], [0, 2]], [[0, 1], [1, 0]], [[0, 1], [1, 1]], [[0, 1], [1, 2]], [[0, 1], [2, 0]], [[0, 1], [2, 1]], [[0, 1], [2, 2]], [[0, 2], [0, 0]], [[0, 2], [0, 1]], [[0, 2], [0, 2]], [[0, 2], [1, 0]], [[0, 2], [1, 1]], [[0, 2], [1, 2]], [[0, 2], [2, 0]], [[0, 2], [2, 1]], [[0, 2], [2, 2]], [[1, 0], [0, 0]], [[1, 0], [0, 1]], [[1, 0], [0, 2]], [[1, 0], [1, 0]], [[1, 0], [1, 1]], [[1, 0], [1, 2]], [[1, 0], [2, 0]], [[1, 0], [2, 1]], [[1, 0], [2, 2]], [[1, 1], [0, 0]], [[1, 1], [0, 1]], [[1, 1], [0, 2]], [[1, 1], [1, 0]], [[1, 1], [1, 1]], [[1, 1], [1, 2]], [[1, 1], [2, 0]], [[1, 1], [2, 1]], [[1, 1], [2, 2]], [[1, 2], [0, 0]], [[1, 2], [0, 1]], [[1, 2], [0, 2]], [[1, 2], [1, 0]], [[1, 2], [1, 1]], [[1, 2], [1, 2]], [[1, 2], [2, 0]], [[1, 2], [2, 1]], [[1, 2], [2, 2]], [[2, 0], [0, 0]], [[2, 0], [0, 1]], [[2, 0], [0, 2]], [[2, 0], [1, 0]], [[2, 0], [1, 1]], [[2, 0], [1, 2]], [[2, 0], [2, 0]], [[2, 0], [2, 1]], [[2, 0], [2, 2]], [[2, 1], [0, 0]], [[2, 1], [0, 1]], [[2, 1], [0, 2]], [[2, 1], [1, 0]], [[2, 1], [1, 1]], [[2, 1], [1, 2]], [[2, 1], [2, 0]], [[2, 1], [2, 1]], [[2, 1], [2, 2]], [[2, 2], [0, 0]], [[2, 2], [0, 1]], [[2, 2], [0, 2]], [[2, 2], [1, 0]], [[2, 2], [1, 1]], [[2, 2], [1, 2]], [[2, 2], [2, 0]], [[2, 2], [2, 1]], [[2, 2], [2, 2]]]
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement