2016-04-22 25 views
0

我需要找到在pyspark列表中出現最多的項目。 假設我有這樣一個清單:pyspark列表中,找到出現最多的項目

mylist = [a, b, c, s, c, c, s, a, c] 

我需要知道,C是具有最高頻率的一個。

我搜索了一些類似的答案,我想

from collections import Counter 

但不能導入名稱計數器。這是否與pyspark?

回答

1

這個列表是否足夠小(大約< 100k元素),你可以在基本Python中做到這一點?如果是這樣,那麼你甚至不需要Spark。

from collections import Counter 
mylist = ['a', 'b', 'c', 's', 'c', 'c', 's', 'a', 'c'] 
counter = Counter(mylist) 
print(counter.most_common()[:5]) # get the five most common elements 

如果你有一個相當大的元素文件來計數,那麼你可能想要使用Spark。在這種情況下,你甚至不需要collections。查看Spark examples page瞭解一些示例代碼。

至於爲什麼你不能導入Counter,我不確定,特別是因爲collections是一個默認提供的軟件包。嘗試打開一個常規的Python shell並運行import collections,collections.Counter。我運行了完全相同的進口生產線,它爲我工作。

相關問題