“ApplyResult”对象在for循环中不可iterable

2024-04-20 07:50:58 发布

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

错误:

Traceback (most recent call last):
  File "son.py", line 120, in <module>
    `main()`
  File "son.py", line 101, in main
    `temp += item`
TypeError: 'ApplyResult' object is not iterable

代码:

pool = multiprocessing.Pool(processes=int(args.process))
for i in range(int(args.process)):
    result_first_round.append(pool.apply_async(Son_Algorithm, (i,)))
pool.close()
pool.join()
first_temp = []
for res in result_first_round: 
    first_temp.append(res)
    #first_temp.append(res.get()) 


#Second Scan
result_final_round = []
temp = []
for item in first_temp:
    temp += item
temp2 = []
for result in temp:
    if not result in temp2:
        temp2.append(result)
temp_result = temp2

Tags: inpyforlineresresultitemtemp
1条回答
网友
1楼 · 发布于 2024-04-20 07:50:58

似乎要将元素item添加到列表temp。在这种情况下,您需要使用方法append(),如下所示:

temp = []
for item in first_temp:
    temp.append(item)

仅当第二个对象也是列表(或至少是iterable)时,列表的运算符+=才起作用

相关问题 更多 >