Skip to content

Commit 08ea5a0

Browse files
authored
Simpler countByValue using collections' Counter
1 parent 598fcbe commit 08ea5a0

1 file changed

Lines changed: 3 additions & 10 deletions

File tree

python/pyspark/rdd.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from subprocess import Popen, PIPE
2929
from tempfile import NamedTemporaryFile
3030
from threading import Thread
31-
from collections import defaultdict
31+
from collections import defaultdict, Counter
3232
from itertools import chain
3333
from functools import reduce
3434
from math import sqrt, log, isinf, isnan, pow, ceil
@@ -1321,16 +1321,9 @@ def countByValue(self):
13211321
[(1, 2), (2, 3)]
13221322
"""
13231323
def countPartition(iterator):
1324-
counts = defaultdict(int)
1325-
for obj in iterator:
1326-
counts[obj] += 1
1327-
yield counts
1324+
yield Counter(iterator)
13281325

1329-
def mergeMaps(m1, m2):
1330-
for k, v in m2.items():
1331-
m1[k] += v
1332-
return m1
1333-
return self.mapPartitions(countPartition).reduce(mergeMaps)
1326+
return self.mapPartitions(countPartition).reduce(operator.add)
13341327

13351328
def top(self, num, key=None):
13361329
"""

0 commit comments

Comments
 (0)