我使用Python 3.5编辑列表,在本例中为predictions_dict['D']
,包含在字典predictions_dict
中。这是我使用的代码:
import multiprocessing as multip
predictions_dict = {'A': [],
'B': [],
'C': [],
'D': [],
'E': [],
'F': [],
'Def': []}
data = [{'index': 1, 'rank': 'A'}, {'index': 2, 'rank': 'D'}, {'index': 3, 'rank': 'E'}]
prediction = [(1, 'C'), (2, 'D'), (3, 'D')]
def create_predictions_dict(index, rank):
for j in data:
if j['index'] == index:
predictions_dict[rank].append((index, j['rank'], rank))
break
np = multip.cpu_count()
p = multip.Pool(processes=np)
_ = p.starmap(create_predictions_dict, prediction)
p.close()
p.join()
print('final list:', predictions_dict['D'])
当我执行此代码时,我得到的输出是:
final list: []
我不明白为什么,正如我期望的那样:
final list: [(2, 'D', 'D'), (3, 'E', 'D')]
答案 0 :(得分:1)
我已经制定了一个解决方案,这要归功于在评论中,问题被确定为流程不共享状态的事实:
import multiprocessing as multip
predictions_dict = {'A': [],
'B': [],
'C': [],
'D': [],
'E': [],
'F': [],
'Def': []}
data = [{'index': 1, 'rank': 'A'}, {'index': 2, 'rank': 'D'}, {'index': 3, 'rank': 'E'}]
prediction = [(1, 'C'), (2, 'D'), (3, 'D')]
def create_predictions_dict(index, rank):
for j in data:
if j['index'] == index:
return index, j['rank'], rank
np = multip.cpu_count()
p = multip.Pool(processes=np)
sk = p.starmap(create_predictions_dict, prediction)
p.close()
p.join()
for elem in sk:
predictions_dict[elem[2]].append(elem)
print('final list:', predictions_dict['D'])