下面的代码是函数的一部分,但我遇到的问题是在这些行上
# 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)]
这将生成一个列表列表,如下所示:
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]]]
例如,如果我只想修改第二个列表第二项中的第二个值:
print(mecs_states[1], '-', mecs_states[1][1], '-', mecs_states[1][1][1])
mecs_states[1][1][1] = 'A'
不仅更改所需的值,还将其他值列在同一索引位置
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]]]
我错过了什么?我需要列表中的每个列表都是独立的。我猜原因在于itertools的产品,但我不完全理解为什么
解决办法是什么
使用json进行序列化和反序列化
输出
当您构建
mecs_states
列表时,您不是在创建新的内部列表,而是在引用您在mec_capacity_states
中创建的相同的列表。作为一个较小的例子:关于
nested
的内部元素的事情是,它们是相同的列表,通过引用,作为pairs
的元素:因此,当您修改
nested[0][0][0]
时,即修改nested[0][0]
和a
都指向的列表。本质上,这是一个稍微复杂一点的问题,人们在做[[]] * 10
时会遇到这个问题,所有十个列表在内存中都是相同的列表您可以通过等到最后,在构建集合之后,将
itertools.product
提供给您的tuple
转换为list
来解决这个问题。但是,一旦进入嵌套集合的深度超过了两个级别,就应该开始考虑类或其他数据结构是否更适合封装所需的行为只需“克隆”一下
mecs_states
列表项。请参见代码中的copy.deepcopy
输出
相关问题 更多 >
编程相关推荐