Skip to content

Commit bcacab4

Browse files
authored
Minor code rearrangement to group related methods together. (GH-30813)
* Make example more focused with math.prod() * Move comparison tests to the multiset operations section
1 parent a1444f4 commit bcacab4

File tree

1 file changed

+38
-40
lines changed

1 file changed

+38
-40
lines changed

Lib/collections/__init__.py

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -617,11 +617,9 @@ def elements(self):
617617
['A', 'A', 'B', 'B', 'C', 'C']
618618
619619
# Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
620+
>>> import math
620621
>>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
621-
>>> product = 1
622-
>>> for factor in prime_factors.elements(): # loop over factors
623-
... product *= factor # and multiply them
624-
>>> product
622+
>>> math.prod(prime_factors.elements())
625623
1836
626624
627625
Note, if an element's count has been set to zero or is a negative
@@ -718,42 +716,6 @@ def __delitem__(self, elem):
718716
if elem in self:
719717
super().__delitem__(elem)
720718

721-
def __eq__(self, other):
722-
'True if all counts agree. Missing counts are treated as zero.'
723-
if not isinstance(other, Counter):
724-
return NotImplemented
725-
return all(self[e] == other[e] for c in (self, other) for e in c)
726-
727-
def __ne__(self, other):
728-
'True if any counts disagree. Missing counts are treated as zero.'
729-
if not isinstance(other, Counter):
730-
return NotImplemented
731-
return not self == other
732-
733-
def __le__(self, other):
734-
'True if all counts in self are a subset of those in other.'
735-
if not isinstance(other, Counter):
736-
return NotImplemented
737-
return all(self[e] <= other[e] for c in (self, other) for e in c)
738-
739-
def __lt__(self, other):
740-
'True if all counts in self are a proper subset of those in other.'
741-
if not isinstance(other, Counter):
742-
return NotImplemented
743-
return self <= other and self != other
744-
745-
def __ge__(self, other):
746-
'True if all counts in self are a superset of those in other.'
747-
if not isinstance(other, Counter):
748-
return NotImplemented
749-
return all(self[e] >= other[e] for c in (self, other) for e in c)
750-
751-
def __gt__(self, other):
752-
'True if all counts in self are a proper superset of those in other.'
753-
if not isinstance(other, Counter):
754-
return NotImplemented
755-
return self >= other and self != other
756-
757719
def __repr__(self):
758720
if not self:
759721
return f'{self.__class__.__name__}()'
@@ -795,6 +757,42 @@ def __repr__(self):
795757
# (cp >= cq) == (sp >= sq)
796758
# (cp > cq) == (sp > sq)
797759

760+
def __eq__(self, other):
761+
'True if all counts agree. Missing counts are treated as zero.'
762+
if not isinstance(other, Counter):
763+
return NotImplemented
764+
return all(self[e] == other[e] for c in (self, other) for e in c)
765+
766+
def __ne__(self, other):
767+
'True if any counts disagree. Missing counts are treated as zero.'
768+
if not isinstance(other, Counter):
769+
return NotImplemented
770+
return not self == other
771+
772+
def __le__(self, other):
773+
'True if all counts in self are a subset of those in other.'
774+
if not isinstance(other, Counter):
775+
return NotImplemented
776+
return all(self[e] <= other[e] for c in (self, other) for e in c)
777+
778+
def __lt__(self, other):
779+
'True if all counts in self are a proper subset of those in other.'
780+
if not isinstance(other, Counter):
781+
return NotImplemented
782+
return self <= other and self != other
783+
784+
def __ge__(self, other):
785+
'True if all counts in self are a superset of those in other.'
786+
if not isinstance(other, Counter):
787+
return NotImplemented
788+
return all(self[e] >= other[e] for c in (self, other) for e in c)
789+
790+
def __gt__(self, other):
791+
'True if all counts in self are a proper superset of those in other.'
792+
if not isinstance(other, Counter):
793+
return NotImplemented
794+
return self >= other and self != other
795+
798796
def __add__(self, other):
799797
'''Add counts from two counters.
800798

0 commit comments

Comments
 (0)