3

Can anyone convert this very simple scala code to 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()
3
  • 1
    What do you expect the output of the code to be? I would guess that code is counting word occurrences, right? So is the expected output { "one" : 1, "two" : 2, "three" : 3} ? Commented Jun 12, 2015 at 20:34
  • 1
    import collections;words = ["one", "two", "two", "three", "three", "three"];collections.Counter(words) would work if { "one" : 1, "two" : 2, "three" : 3} is what you want. Commented Jun 12, 2015 at 20:38
  • yes i'm expecting output like this : [('one', 1), ('two', 3), ('three', 3)]. what will be the python code for .map(t => (t._1, t._2.sum)) line? Commented Jun 12, 2015 at 20:45

3 Answers 3

5

try this:

words = ["one", "two", "two", "three", "three", "three"]
wordPairsRDD = sc.parallelize(words).map(lambda word : (word, 1))

wordCountsWithGroup = wordPairsRDD
    .groupByKey()
    .map(lambda t: (t[0], sum(t[1])))
    .collect()
Sign up to request clarification or add additional context in comments.

Comments

2

Two translate in python :

from operator import add
wordsList = ["one", "two", "two", "three", "three", "three"]
words = sc.parallelize(wordsList ).map(lambda l :(l,1)).reduceByKey(add).collect()
print words
words = sc.parallelize(wordsList ).map(lambda l : (l,1)).groupByKey().map(lambda t: (t[0], sum(t[1]))).collect()
print words

Comments

2

Assuming you already have a Spark context defined and ready to go:

 from operator import add
 words = ["one", "two", "two", "three", "three", "three"]
 wordsPairRDD = sc.parallelize(words).map(lambda word: (word, 1))
      .reduceByKey(add)
      .collect()

Checkout the github examples repo: Python Examples

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.