Skip to content

Commit

Permalink
doctest updated for mapValues, flatMapValues in rdd.py
Browse files Browse the repository at this point in the history
Updated doctests for mapValues and flatMapValues in rdd.py

Author: jyotiska <[email protected]>

Closes apache#621 from jyotiska/python_spark and squashes the following commits:

716f7cd [jyotiska] doctest updated for mapValues, flatMapValues in rdd.py
  • Loading branch information
jyotiska authored and aarondav committed Feb 22, 2014
1 parent 3ff077d commit 722199f
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/pyspark/rdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,11 @@ def flatMapValues(self, f):
Pass each value in the key-value pair RDD through a flatMap function
without changing the keys; this also retains the original RDD's
partitioning.
>>> x = sc.parallelize([("a", ["x", "y", "z"]), ("b", ["p", "r"])])
>>> def f(x): return x
>>> x.flatMapValues(f).collect()
[('a', 'x'), ('a', 'y'), ('a', 'z'), ('b', 'p'), ('b', 'r')]
"""
flat_map_fn = lambda (k, v): ((k, x) for x in f(v))
return self.flatMap(flat_map_fn, preservesPartitioning=True)
Expand All @@ -955,6 +960,11 @@ def mapValues(self, f):
Pass each value in the key-value pair RDD through a map function
without changing the keys; this also retains the original RDD's
partitioning.
>>> x = sc.parallelize([("a", ["apple", "banana", "lemon"]), ("b", ["grapes"])])
>>> def f(x): return len(x)
>>> x.mapValues(f).collect()
[('a', 3), ('b', 1)]
"""
map_values_fn = lambda (k, v): (k, f(v))
return self.map(map_values_fn, preservesPartitioning=True)
Expand Down

0 comments on commit 722199f

Please sign in to comment.