AttributeError:“ListProxy”对象没有属性“copy”

2024-10-01 09:28:45 发布

您现在位置:Python中文网/ 问答频道 /正文

Python 3.5

我正在尝试并行化下面的代码,为此我使用了来自多处理模块的ListProxy对象,以便访问该列表的工作人员以一种受管理的方式执行此操作。我知道来自多处理模块的代理对象是不可编辑的,但是您可以调用对象上的copy()函数来返回该对象的常规版本,即DictProxy上的copy()返回一个可以迭代的常规Dict。在

def getAllLabels(graph: UndirectedGraph, graphSize: int, probabilityList: list) -> list:
'''Enumerate through ESU algorithm and retrieve all subgraphs of a given size for this graph.'''

process_manager = multiprocessing.Manager()
pool = multiprocessing.Pool(4)
subgraphList = process_manager.list() #looped
exclusionList = list() #looped

for i in range(0, graph.getSize()):
    nodeList = list([i])
    neighborList = list() #looped
    for n in graph.getAdjacencyList(i):
        if n not in exclusionList and n not in nodeList and n not in neighborList:
            neighborList.append(n)

    #pool.apply_async(getSubgraphs, [graph, graphSize, nodeList, neighborList, exclusionList, subgraphList, probabilityList])
    getSubgraphs(graph, graphSize, nodeList, neighborList, exclusionList, subgraphList.copy(), probabilityList)
    exclusionList.append(i)

labels = list()
for g in subgraphList.copy():
    labels.append(nodesToGraph(graph, g))

pool.close()
return labels

但是,每次对列表对象调用copy()时,都会出现以下错误:

^{pr2}$

Tags: and对象inforlistgraphpoolcopy
2条回答

尝试使用

subgraphList[:]

而不是子图列表.复制()作为子GraphList,可能没有以这种方式实现.copy()方法。在

否则,可选择:

^{pr2}$

如果使用dir(subgraphList),则可以看到LitProxy对象具有的可能方法

['_Client', '__add__', '__builtins__', '__class__', '__contains__', '__deepcopy__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_address_to_local', '_after_fork', '_authkey', '_callmethod', '_close', '_connect', '_decref', '_exposed_', '_getvalue', '_id', '_idset', '_incref', '_manager', '_mutex', '_owned_by_manager', '_serializer', '_tls', '_token', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

如您所见,它有一个可以使用的deepcopy方法

^{pr2}$

此外,deepcopy还确保复制甚至嵌套的项

相关问题 更多 >