summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-01-22 09:09:55 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-01-22 09:09:55 (GMT)
commitdd01f8f37b98af090098495200107dfaf92a3162 (patch)
tree18c0b16d82372a0fd8ffbbafbf70ef18536dda3a /Lib
parent94adc8e4b54d6532dd8a4b265133d4105b96ea7a (diff)
downloadcpython-dd01f8f37b98af090098495200107dfaf92a3162.zip
cpython-dd01f8f37b98af090098495200107dfaf92a3162.tar.gz
cpython-dd01f8f37b98af090098495200107dfaf92a3162.tar.bz2
Update comments and add an optimized path for Counter.update().
Diffstat (limited to 'Lib')
-rw-r--r--Lib/collections.py8
1 files changed, 5 insertions, 3 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 232c772..6831cf1 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -255,8 +255,11 @@ class Counter(dict):
if iterable is not None:
if isinstance(iterable, Mapping):
- for elem, count in iterable.items():
- self[elem] += count
+ if self:
+ for elem, count in iterable.items():
+ self[elem] += count
+ else:
+ dict.update(self, iterable) # fast path when counter is empty
else:
for elem in iterable:
self[elem] += 1
@@ -282,7 +285,6 @@ class Counter(dict):
# Knuth TAOCP Volume II section 4.6.3 exercise 19
# and at http://en.wikipedia.org/wiki/Multiset
#
- # Results are undefined when inputs contain negative counts.
# Outputs guaranteed to only include positive counts.
#
# To strip negative and zero counts, add-in an empty counter: