任何人都可以將此非常簡單的scala代碼轉換爲python嗎?將Apache Spark Scala代碼轉換爲Python
val words = Array("one", "two", "two", "three", "three", "three")
val wordPairsRDD = sc.parallelize(words).map(word => (word, 1))
val wordCountsWithGroup = wordPairsRDD
.groupByKey()
.map(t => (t._1, t._2.sum))
.collect()
你認爲代碼的輸出是什麼?我猜想代碼是在計算單詞出現次數,對嗎?那麼預期結果{「one」:1,「two」:2,「three」:3}? – eugenioy
'import collections; words = [「one」,「two」,「two」,「three」,「three」,「three」]; collections.Counter(words)''if''「one」:1 ,「two」:2,「three」:3}'就是你想要的。 –
是的,我期待這樣的輸出:[('one',1),('two',3),('three',3)]。 .map(t =>(t._1,t._2.sum))行的Python代碼是什麼? – muktadiur