如何解决itertools.product() 的替代品?
我有以下代码:
# 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]],
我一直在使用 copy.deepcopy() 来避免这种情况,但我认为问题出在代码行中:
mecs_states = [copy.deepcopy(list(p)) for p in itertools.product(mec_capacity_states,repeat=NUM_MECS)]
这个问题发生在子列表相等的任何组合中。
我可以使用任何替代品吗?
解决方法
使用 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 方法仅在数据结构表现良好时才有效。它必须是非循环的,子元素必须是简单的类型,例如列表、整数、字符串等。
序列化和反序列化您的列表列表,如在 @HåkenLid 的 answer 中,如果 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,1]],2]],[1,...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。