summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-01-22 09:05:43 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-01-22 09:05:43 (GMT)
commit1bc1c8ac766e62116bd9d677712c59d681e44c0e (patch)
treecb1344203d3102f9d2c1cbd14c1fa9c703069dce
parent7bdca05a3b4dd0e3cd607d35ee597e8ed177b850 (diff)
downloadcpython-1bc1c8ac766e62116bd9d677712c59d681e44c0e.zip
cpython-1bc1c8ac766e62116bd9d677712c59d681e44c0e.tar.gz
cpython-1bc1c8ac766e62116bd9d677712c59d681e44c0e.tar.bz2
Update comments and add an optimized path for Counter.update().
-rw-r--r--Lib/collections.py8
1 files changed, 5 insertions, 3 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index eb13f4d2..effae7d 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -253,8 +253,11 @@ class Counter(dict):
if iterable is not None:
if isinstance(iterable, Mapping):
- for elem, count in iterable.iteritems():
- self[elem] += count
+ if self:
+ for elem, count in iterable.iteritems():
+ self[elem] += count
+ else:
+ dict.update(self, iterable) # fast path when counter is empty
else:
for elem in iterable:
self[elem] += 1
@@ -280,7 +283,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: