recipes.pyx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from cpython.sequence cimport PySequence_Tuple
  2. from cytoolz.itertoolz cimport frequencies, pluck
  3. from itertools import groupby
  4. __all__ = ['countby', 'partitionby']
  5. cpdef object countby(object key, object seq):
  6. """
  7. Count elements of a collection by a key function
  8. >>> countby(len, ['cat', 'mouse', 'dog'])
  9. {3: 2, 5: 1}
  10. >>> def iseven(x): return x % 2 == 0
  11. >>> countby(iseven, [1, 2, 3]) # doctest:+SKIP
  12. {True: 1, False: 2}
  13. See Also:
  14. groupby
  15. """
  16. if not callable(key):
  17. return frequencies(pluck(key, seq))
  18. return frequencies(map(key, seq))
  19. cdef class partitionby:
  20. """ partitionby(func, seq)
  21. Partition a sequence according to a function
  22. Partition `s` into a sequence of lists such that, when traversing
  23. `s`, every time the output of `func` changes a new list is started
  24. and that and subsequent items are collected into that list.
  25. >>> is_space = lambda c: c == " "
  26. >>> list(partitionby(is_space, "I have space"))
  27. [('I',), (' ',), ('h', 'a', 'v', 'e'), (' ',), ('s', 'p', 'a', 'c', 'e')]
  28. >>> is_large = lambda x: x > 10
  29. >>> list(partitionby(is_large, [1, 2, 1, 99, 88, 33, 99, -1, 5]))
  30. [(1, 2, 1), (99, 88, 33, 99), (-1, 5)]
  31. See also:
  32. partition
  33. groupby
  34. itertools.groupby
  35. """
  36. def __cinit__(self, object func, object seq):
  37. self.iter_groupby = groupby(seq, key=func)
  38. def __iter__(self):
  39. return self
  40. def __next__(self):
  41. cdef object key, val
  42. key, val = next(self.iter_groupby)
  43. return PySequence_Tuple(val)