簡體   English   中英

如何在Python計數器中使用most_common?

[英]How do I use most_common in a Python counter?

我有一個功能旨在使我的數據中出現的單詞的底部n個百分比最低。 該函數是:

def bottomnpercent(table,n):

words=0
wordcounter=Counter()
for key, data in table.scan():
    if not key in stopwords:
        words+=1
        wordcounter[key]+= getsomedata 
        idx=percentage(n,words)
return Counter(wordcounter.most_common()[-idx:])

(table.scan循環通過具有單詞和頻率計數的HBASE表; getsomedata進行查找以返回特定單詞的計數)。

問題是這將返回以下形式的計數器:

Counter({('stopped', 173): 1, ('thrilling', 17): 1, ('fluids', 18): 1, ('Pictures', 18): 1, ('steering', 37): 1,... 

這是不好的,因為一切都會發生1次,而我需要這樣的東西:

 Counter({('stopped'): 173, ('thrilling'): 17, ('fluids'): 18, ('Pictures'): 18, ('steering'): 37,... 

但我不知道怎么做。 任何幫助深表感謝。 TIA!

這是因為wordcounter是一個計數器( wordcounter=Counter() ),您再次在另一個計數器中使用它return Counter(wordcounter.most_common()[-idx:]) 您只需要返回以下內容:

return wordcounter.most_common()[-idx:]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM