syntax philosophy

Peter Otten __peter__ at web.de
Mon Nov 17 17:42:19 EST 2003


No direct answer, but if you need histograms that often, you might consider
putting something along these lines into your toolbox:

import copy
class histogram(dict):
    def __init__(self, default):
        dict.__init__(self)
        self.default = default
    def __getitem__(self, key):
        return self.setdefault(key, copy.copy(self.default))


h = histogram(0)
sample = [(1, 10), (2, 10), (0, 1), (1, 1)]
for key, freq in sample:
    h[key] += freq
print h

h = histogram([])
for key, item in sample:
    h[key].append(item)
print h

While 0 and 0.0 are certainly the most frequent default values, they are not
the only options.

Peter




More information about the Python-list mailing list