Python多处理返回di

2024-05-20 14:10:03 发布

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

我想并行化一个函数,该函数在dict中返回一个平坦的值列表(称为“keys”),但我不知道如何获得最终结果。我试过:

def toParallel(ht, token):
    keys = []
    words = token[token['hashtag'] == ht]['word']
    for w in words:
        keys.append(checkString(w))
    y = {ht:keys}

num_cores = multiprocessing.cpu_count()
pool = multiprocessing.Pool(num_cores)

token = pd.read_csv('/path', sep=",", header = None, encoding='utf-8')
token.columns = ['word', 'hashtag', 'count']
hashtag = pd.DataFrame(token.groupby(by='hashtag', as_index=False).count()['hashtag'])

result = pd.DataFrame(index = hashtag['hashtag'], columns = range(0, 21))
result = result.fillna(0)

final_result = []
final_result = [pool.apply_async(toParallel, args=(ht,token,)) for ht in hashtag['hashtag']]

Where toParallel函数应该返回一个以hashtag作为key的dict和一个键列表(其中keys是int)。但如果我试图打印最终结果,我只得到

bound method ApplyResult.get of multiprocessing.pool.ApplyResult object at 0x10c4fa950

我该怎么做?在


Tags: 函数token列表countresultkeysmultiprocessingdict
3条回答

差别是operator precedence+优先于&,但&优先于+=。因此,您的操作转化为:

n = (n + 0) & 1; // = n & 1 = 0 (when n is even)
n += (0 & 1);    // = n + 0 = n

在第一种情况下,n将首先加上0,然后再加上1

n = n+0&1 -> n = (n + 0)&1 -> 0
n+=0&1 ->n+=(0&1)->n

运营者及;优先级低于+

你实际上写的是:

n = ( n + 0 ) &1;

我加了括号来澄清

因为n是偶数,所以该表达式的结果为零

相关问题 更多 >