diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2022-01-23 00:47:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-23 00:47:22 (GMT) |
commit | bcacab47bf9e8bee58f6f248638e229ae8ea7992 (patch) | |
tree | 71179d7e489fad1174caebe7468760d4598a4da7 /Lib/collections/__init__.py | |
parent | a1444f43584af0f7a0af72aa06ba0a86ae5a87a2 (diff) | |
download | cpython-bcacab47bf9e8bee58f6f248638e229ae8ea7992.zip cpython-bcacab47bf9e8bee58f6f248638e229ae8ea7992.tar.gz cpython-bcacab47bf9e8bee58f6f248638e229ae8ea7992.tar.bz2 |
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
Diffstat (limited to 'Lib/collections/__init__.py')
-rw-r--r-- | Lib/collections/__init__.py | 78 |
1 files changed, 38 insertions, 40 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index d989d85..fa8b309 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -617,11 +617,9 @@ class Counter(dict): ['A', 'A', 'B', 'B', 'C', 'C'] # Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1 + >>> import math >>> prime_factors = Counter({2: 2, 3: 3, 17: 1}) - >>> product = 1 - >>> for factor in prime_factors.elements(): # loop over factors - ... product *= factor # and multiply them - >>> product + >>> math.prod(prime_factors.elements()) 1836 Note, if an element's count has been set to zero or is a negative @@ -718,42 +716,6 @@ class Counter(dict): if elem in self: super().__delitem__(elem) - def __eq__(self, other): - 'True if all counts agree. Missing counts are treated as zero.' - if not isinstance(other, Counter): - return NotImplemented - return all(self[e] == other[e] for c in (self, other) for e in c) - - def __ne__(self, other): - 'True if any counts disagree. Missing counts are treated as zero.' - if not isinstance(other, Counter): - return NotImplemented - return not self == other - - def __le__(self, other): - 'True if all counts in self are a subset of those in other.' - if not isinstance(other, Counter): - return NotImplemented - return all(self[e] <= other[e] for c in (self, other) for e in c) - - def __lt__(self, other): - 'True if all counts in self are a proper subset of those in other.' - if not isinstance(other, Counter): - return NotImplemented - return self <= other and self != other - - def __ge__(self, other): - 'True if all counts in self are a superset of those in other.' - if not isinstance(other, Counter): - return NotImplemented - return all(self[e] >= other[e] for c in (self, other) for e in c) - - def __gt__(self, other): - 'True if all counts in self are a proper superset of those in other.' - if not isinstance(other, Counter): - return NotImplemented - return self >= other and self != other - def __repr__(self): if not self: return f'{self.__class__.__name__}()' @@ -795,6 +757,42 @@ class Counter(dict): # (cp >= cq) == (sp >= sq) # (cp > cq) == (sp > sq) + def __eq__(self, other): + 'True if all counts agree. Missing counts are treated as zero.' + if not isinstance(other, Counter): + return NotImplemented + return all(self[e] == other[e] for c in (self, other) for e in c) + + def __ne__(self, other): + 'True if any counts disagree. Missing counts are treated as zero.' + if not isinstance(other, Counter): + return NotImplemented + return not self == other + + def __le__(self, other): + 'True if all counts in self are a subset of those in other.' + if not isinstance(other, Counter): + return NotImplemented + return all(self[e] <= other[e] for c in (self, other) for e in c) + + def __lt__(self, other): + 'True if all counts in self are a proper subset of those in other.' + if not isinstance(other, Counter): + return NotImplemented + return self <= other and self != other + + def __ge__(self, other): + 'True if all counts in self are a superset of those in other.' + if not isinstance(other, Counter): + return NotImplemented + return all(self[e] >= other[e] for c in (self, other) for e in c) + + def __gt__(self, other): + 'True if all counts in self are a proper superset of those in other.' + if not isinstance(other, Counter): + return NotImplemented + return self >= other and self != other + def __add__(self, other): '''Add counts from two counters. |