itertools.product() 的替代品?

时间:2021-03-07 21:32:03

标签: python itertools variations

我有以下代码:

# Mec capacity to hold a vnf
served_vnfs         = [0,1,2]

# All possible mec states as far as holded vnfs
mec_capacity_states = [copy.deepcopy(list(s)) for s in itertools.product(served_vnfs, repeat=2)]

# All possible states with defined number of mecs
mecs_states = [copy.deepcopy(list(p)) for p in itertools.product(mec_capacity_states, repeat=2)]

当我执行以下操作时:

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

我得到的第一个值是:

[['A', 0], ['A', 0]]

这不是应该的,而是应该的:

[['A', 0], [0, 0]],

我一直在使用 copy.deepcopy() 来避免这种情况,但我认为问题出在代码行中:

mecs_states = [copy.deepcopy(list(p)) for p in itertools.product(mec_capacity_states, repeat=NUM_MECS)]

这个问题发生在子列表相等的任何组合中。

我可以使用任何替代品吗?

2 个答案:

答案 0 :(得分:2)

使用 deepcopy 将为您提供一个数据结构,其中原始对象中的任何共享引用也反映在副本中。

如果要删除任何共享引用,可以使用 json 进行序列化和反序列化。

import json

# All possible states with defined number of mecs
mecs_states = itertools.product(mec_capacity_states, repeat=2)
mecs_states = json.loads(json.dumps(list(mecs_states)))

请注意,deepcopy 可用于任何 Python 数据结构。 json 方法仅在数据结构表现良好时才有效。它必须是非循环的,子元素必须是简单的类型,例如列表、整数、字符串等。

答案 1 :(得分:2)

序列化和反序列化您的列表列表,如在 @HåkenLidanswer 中,如果 served_vnfs 中的元素是简单的可序列化对象(例如整数) '不需要保留身份。但这似乎有很多不必要的工作,尤其是在列表可能更长的情况下。

一种可能更简单的方法是复制从 itertools.product 的外部调用返回的每个列表。由于这只是列表的浅拷贝,因此无需使用 copy 模块:

served_vnfs         = [0,1,2]
mec_capacity_states = list(itertools.product(served_vnfs, repeat=2))
mecs_states         = [list(map(list, p))
                       for p in itertools.product(mec_capacity_states,
                                                  repeat=2)]

测试(截断以避免滚动):

>>> mecs_states[0][0][0] = 'a'
>>> mecs_states
[[['a', 0], [0, 0]], [[0, 0], [0, 1]], [[0, 0], [0, 2]], [[0, 0], [1, 0]], ...
相关问题