Iterator over elements repeating each as many times as its count. >>> c = Counter('ABCABC') >>> sorted(c.elements()) ['A', 'A', 'B', 'B', 'C', 'C'] Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1 >>> import math >>> prime_factors = C
(self)
| 637 | return _nlargest(n, self.items(), key=_itemgetter(1)) |
| 638 | |
| 639 | def elements(self): |
| 640 | '''Iterator over elements repeating each as many times as its count. |
| 641 | |
| 642 | >>> c = Counter('ABCABC') |
| 643 | >>> sorted(c.elements()) |
| 644 | ['A', 'A', 'B', 'B', 'C', 'C'] |
| 645 | |
| 646 | Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1 |
| 647 | |
| 648 | >>> import math |
| 649 | >>> prime_factors = Counter({2: 2, 3: 3, 17: 1}) |
| 650 | >>> math.prod(prime_factors.elements()) |
| 651 | 1836 |
| 652 | |
| 653 | Note, if an element's count has been set to zero or is a negative |
| 654 | number, elements() will ignore it. |
| 655 | |
| 656 | ''' |
| 657 | # Emulate Bag.do from Smalltalk and Multiset.begin from C++. |
| 658 | return _chain.from_iterable(_starmap(_repeat, self.items())) |
| 659 | |
| 660 | # Override dict methods where necessary |
| 661 |