mirror of
https://github.com/python/cpython.git
synced 2025-07-30 06:34:15 +00:00
Update comments and add an optimized path for Counter.update().
This commit is contained in:
parent
7bdca05a3b
commit
1bc1c8ac76
1 changed files with 5 additions and 3 deletions
|
@ -253,8 +253,11 @@ class Counter(dict):
|
||||||
|
|
||||||
if iterable is not None:
|
if iterable is not None:
|
||||||
if isinstance(iterable, Mapping):
|
if isinstance(iterable, Mapping):
|
||||||
for elem, count in iterable.iteritems():
|
if self:
|
||||||
self[elem] += count
|
for elem, count in iterable.iteritems():
|
||||||
|
self[elem] += count
|
||||||
|
else:
|
||||||
|
dict.update(self, iterable) # fast path when counter is empty
|
||||||
else:
|
else:
|
||||||
for elem in iterable:
|
for elem in iterable:
|
||||||
self[elem] += 1
|
self[elem] += 1
|
||||||
|
@ -280,7 +283,6 @@ class Counter(dict):
|
||||||
# Knuth TAOCP Volume II section 4.6.3 exercise 19
|
# Knuth TAOCP Volume II section 4.6.3 exercise 19
|
||||||
# and at http://en.wikipedia.org/wiki/Multiset
|
# and at http://en.wikipedia.org/wiki/Multiset
|
||||||
#
|
#
|
||||||
# Results are undefined when inputs contain negative counts.
|
|
||||||
# Outputs guaranteed to only include positive counts.
|
# Outputs guaranteed to only include positive counts.
|
||||||
#
|
#
|
||||||
# To strip negative and zero counts, add-in an empty counter:
|
# To strip negative and zero counts, add-in an empty counter:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue