MCPcopy Index your code
hub / github.com/python/cpython / groupby

Method groupby

Lib/test/test_itertools.py:1665–1695  ·  view source on GitHub ↗
(iterable, key=None)

Source from the content-addressed store, hash-verified

1663 # Begin groupby() recipe #######################################
1664
1665 def groupby(iterable, key=None):
1666 # [k for k, g in groupby('AAAABBBCCDAABBB')] → A B C D A B
1667 # [list(g) for k, g in groupby('AAAABBBCCD')] → AAAA BBB CC D
1668
1669 keyfunc = (lambda x: x) if key is None else key
1670 iterator = iter(iterable)
1671 exhausted = False
1672
1673 def _grouper(target_key):
1674 nonlocal curr_value, curr_key, exhausted
1675 yield curr_value
1676 for curr_value in iterator:
1677 curr_key = keyfunc(curr_value)
1678 if curr_key != target_key:
1679 return
1680 yield curr_value
1681 exhausted = True
1682
1683 try:
1684 curr_value = next(iterator)
1685 except StopIteration:
1686 return
1687 curr_key = keyfunc(curr_value)
1688
1689 while not exhausted:
1690 target_key = curr_key
1691 curr_group = _grouper(target_key)
1692 yield curr_key, curr_group
1693 if curr_key == target_key:
1694 for _ in curr_group:
1695 pass
1696
1697 # End groupby() recipe #########################################
1698

Callers 7

output_lineMethod · 0.80
set_match_testsFunction · 0.80
iterdirMethod · 0.80
deprecate_keyword_useMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected