2017-09-03 96 views
0

我需要根据以下实现的频率计数对词进行排序。清洁的停止词后基于值排序

分裂的话:

words=Counter([item for sublist in m.split('\W+') for item in word_tokenize(sublist)]) 

频率计数:

wordsFreq=['%s: %d' %(x, words[x]) for x in words] 

输出:

["limited: 1", "desirable: 1", "advices: 1","new: 8", "net: 5", "increasing: 2",......] 

print type(wordsFreq) 

输出

<type 'list'> 
+2

你的问题是什么?这似乎只是你写的内容的陈述。 – Carcigenicate

+0

Carcgenicate - 我需要根据频率计数对输出列表进行排序。为一个实例限制:1,合意:1,建议:1,增加:2,新:5,新:8 .... – lpt

+0

给这个很好的阅读:https://wiki.python.org/moin/方法文档/排序。 'sort'有一个可选参数,可以让你选择排序的元素。你可能不希望它们在排序前被制作成字符串。这会让事情变得复杂。 – Carcigenicate

回答

0

一种方式做到这一点是将数据转换成文字的按键和频率的字典作为值:

import operator 

in_lst = ["limited: 1", "desirable: 1", "advices: 1", 
      "new: 8", "net: 5", "increasing: 2"] 

freq_dict = {x[0]: x[1] for x in [i.split(": ") for i in in_lst]} 

sorted_lst = sorted(freq_dict.items(), key=operator.itemgetter(1)) 

out_lst = [": ".join(i) for i in sorted_lst] 

这个程序,然后根据订单在字典中的值的项目。 sorted_lst是元组列表,然后将其转换为原始字符串列表,按升序对其频率进行排序。

另一种解决方案是使用collections模块中的OrderedDict

+0

这是一个很好的例子,atru。谢谢 – lpt

+0

@lpt - 不客气! – atru