使用分组计算列表中的单词频率

时间:2018-03-09 14:13:07

标签: python

我有计算单词频率的代码(前3位),但该值的结果作为元组返回。

有没有办法改进这段代码?

result:
defaultdict(<class 'dict'>, {'first': [('earth', 2), ('Jellicle', 2), 
('surface', 2)], 'second': [('first', 2), ('university', 2), ('north', 2)]})


from collections import defaultdict, Counter
words = [
['earth total surface area land Jellicle ', 'first']
,['university earth surface pleasant Jellicle ', 'first']
,['first university east france north ', 'second']
,['first north university ', 'second']
]

result = defaultdict(list)
for row in words:
    sstr = list(row)
    words = [sstr[0],sstr[1]]
    temp = words[0].split()
    for i in range(len(temp)):
        result[words[1]].append(temp[i])
result_finish = defaultdict(dict)
for key in result.keys():
    temp_dict = {key: Counter(result[key]).most_common(3)}
    result_finish.update(temp_dict)
print(result_finish) 

2 个答案:

答案 0 :(得分:1)

一种方法是通过pandas按类别汇总,然后使用collections.Counter

import pandas as pd
from collections import Counter

words = [['earth total surface area land Jellicle ', 'first'],
         ['university earth surface pleasant Jellicle ', 'first'],
         ['first university east france north ', 'second'],
         ['first north university ', 'second']]

df = pd.DataFrame(words).groupby(1)[0].apply(lambda x: x.sum())

result = {df.index[i]: Counter(df.iloc[i].split(' ')).most_common(3) \
                       for i in range(len(df.index))}

# {'first': [('earth', 2), ('surface', 2), ('Jellicle', 2)],
#  'second': [('first', 2), ('university', 2), ('north', 2)]}

答案 1 :(得分:1)

这是使用dict理解的代码的较短(希望更清晰)版本:

result = defaultdict(list)

for sentence, key in words:
    result[key].extend(sentence.split())

result_count = {k: Counter(v).most_common(3) for k,v in result.items()}

>> result_count: 
>> {'first': [('earth', 2), ('surface', 2), ('Jellicle', 2)], 
>>  'second': [('first', 2), ('university', 2), ('north', 2)]}

如果你想要它没有计数元组:

result = {k: [w for w,_ in Counter(v).most_common(3)] for k,v in result.items()}

>> result_without_count_per_word
>> {'first': ['earth', 'surface', 'Jellicle'], 
>>  'second': ['first', 'university', 'north']}