@@ -240,11 +240,19 @@ def pop(self, key, default=__marker):
240
240
is raised.
241
241
242
242
'''
243
- if key in self :
244
- result = self [key ]
245
- del self [key ]
243
+ marker = self .__marker
244
+ result = dict .pop (self , key , marker )
245
+ if result is not marker :
246
+ # The same as in __delitem__().
247
+ link = self .__map .pop (key )
248
+ link_prev = link .prev
249
+ link_next = link .next
250
+ link_prev .next = link_next
251
+ link_next .prev = link_prev
252
+ link .prev = None
253
+ link .next = None
246
254
return result
247
- if default is self . __marker :
255
+ if default is marker :
248
256
raise KeyError (key )
249
257
return default
250
258
@@ -267,10 +275,22 @@ def __repr__(self):
267
275
268
276
def __reduce__ (self ):
269
277
'Return state information for pickling'
270
- inst_dict = vars (self ).copy ()
271
- for k in vars (OrderedDict ()):
272
- inst_dict .pop (k , None )
273
- return self .__class__ , (), inst_dict or None , None , iter (self .items ())
278
+ state = self .__getstate__ ()
279
+ if state :
280
+ if isinstance (state , tuple ):
281
+ state , slots = state
282
+ else :
283
+ slots = {}
284
+ state = state .copy ()
285
+ slots = slots .copy ()
286
+ for k in vars (OrderedDict ()):
287
+ state .pop (k , None )
288
+ slots .pop (k , None )
289
+ if slots :
290
+ state = state , slots
291
+ else :
292
+ state = state or None
293
+ return self .__class__ , (), state , None , iter (self .items ())
274
294
275
295
def copy (self ):
276
296
'od.copy() -> a shallow copy of od'
@@ -613,11 +633,9 @@ def elements(self):
613
633
['A', 'A', 'B', 'B', 'C', 'C']
614
634
615
635
# Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
636
+ >>> import math
616
637
>>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
617
- >>> product = 1
618
- >>> for factor in prime_factors.elements(): # loop over factors
619
- ... product *= factor # and multiply them
620
- >>> product
638
+ >>> math.prod(prime_factors.elements())
621
639
1836
622
640
623
641
Note, if an element's count has been set to zero or is a negative
@@ -714,42 +732,6 @@ def __delitem__(self, elem):
714
732
if elem in self :
715
733
super ().__delitem__ (elem )
716
734
717
- def __eq__ (self , other ):
718
- 'True if all counts agree. Missing counts are treated as zero.'
719
- if not isinstance (other , Counter ):
720
- return NotImplemented
721
- return all (self [e ] == other [e ] for c in (self , other ) for e in c )
722
-
723
- def __ne__ (self , other ):
724
- 'True if any counts disagree. Missing counts are treated as zero.'
725
- if not isinstance (other , Counter ):
726
- return NotImplemented
727
- return not self == other
728
-
729
- def __le__ (self , other ):
730
- 'True if all counts in self are a subset of those in other.'
731
- if not isinstance (other , Counter ):
732
- return NotImplemented
733
- return all (self [e ] <= other [e ] for c in (self , other ) for e in c )
734
-
735
- def __lt__ (self , other ):
736
- 'True if all counts in self are a proper subset of those in other.'
737
- if not isinstance (other , Counter ):
738
- return NotImplemented
739
- return self <= other and self != other
740
-
741
- def __ge__ (self , other ):
742
- 'True if all counts in self are a superset of those in other.'
743
- if not isinstance (other , Counter ):
744
- return NotImplemented
745
- return all (self [e ] >= other [e ] for c in (self , other ) for e in c )
746
-
747
- def __gt__ (self , other ):
748
- 'True if all counts in self are a proper superset of those in other.'
749
- if not isinstance (other , Counter ):
750
- return NotImplemented
751
- return self >= other and self != other
752
-
753
735
def __repr__ (self ):
754
736
if not self :
755
737
return f'{ self .__class__ .__name__ } ()'
@@ -795,6 +777,42 @@ def __repr__(self):
795
777
# (cp >= cq) == (sp >= sq)
796
778
# (cp > cq) == (sp > sq)
797
779
780
+ def __eq__ (self , other ):
781
+ 'True if all counts agree. Missing counts are treated as zero.'
782
+ if not isinstance (other , Counter ):
783
+ return NotImplemented
784
+ return all (self [e ] == other [e ] for c in (self , other ) for e in c )
785
+
786
+ def __ne__ (self , other ):
787
+ 'True if any counts disagree. Missing counts are treated as zero.'
788
+ if not isinstance (other , Counter ):
789
+ return NotImplemented
790
+ return not self == other
791
+
792
+ def __le__ (self , other ):
793
+ 'True if all counts in self are a subset of those in other.'
794
+ if not isinstance (other , Counter ):
795
+ return NotImplemented
796
+ return all (self [e ] <= other [e ] for c in (self , other ) for e in c )
797
+
798
+ def __lt__ (self , other ):
799
+ 'True if all counts in self are a proper subset of those in other.'
800
+ if not isinstance (other , Counter ):
801
+ return NotImplemented
802
+ return self <= other and self != other
803
+
804
+ def __ge__ (self , other ):
805
+ 'True if all counts in self are a superset of those in other.'
806
+ if not isinstance (other , Counter ):
807
+ return NotImplemented
808
+ return all (self [e ] >= other [e ] for c in (self , other ) for e in c )
809
+
810
+ def __gt__ (self , other ):
811
+ 'True if all counts in self are a proper superset of those in other.'
812
+ if not isinstance (other , Counter ):
813
+ return NotImplemented
814
+ return self >= other and self != other
815
+
798
816
def __add__ (self , other ):
799
817
'''Add counts from two counters.
800
818
0 commit comments