2017-09-03 154 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