[issue25478] Consider adding a normalize() method to collections.Counter()
Raymond Hettinger
report at bugs.python.org
Sun Dec 20 15:44:05 EST 2020
Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:
Here's what I propose to add:
def total(self):
return sum(self.values())
def scaled_by(self, factor):
return Counter({elem : count * factor for elem, count in self.items()})
def scaled_to(self, target_total=1.0):
ratio = target_total / self.total()
return self.scaled_by(ratio)
These cover the common cases and they don't mutate the counter.
----------
versions: +Python 3.10 -Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue25478>
_______________________________________
More information about the Python-bugs-list
mailing list