Terry J. Reedy added the comment: The '|' should be '&' to avoid useless summing of 0 products.
I think this should be rejected. Python's number and collections classes provide basic operations from which users can build application-specific functions. Or in the case of floats, we provide a separate module of specialized functions. Or in the case of itertools, a section of recipes that build on the basic iterators in the module. In this case, it makes little sense to me to provide an 'inner product' of Counters (bags, multisets). >>> a, b, c, d = 'a', 'b', 'c', 'd' >>> c1 = C([a,a,a,b,b,c]) >>> c2 = C([a, c,c,c, d,d,d]) >>> sum(c1[x]*c2[x] for x in c1.keys() & c2.keys()) 6 Even if the keys are counts and one thinks of the counters as sparse vectors, they are not really matrices. Hence @ does not exactly fit. If one does want sparse vectors implemented as dicts, they do not always have to be Counters. A function would not require that. def sparse_inner_prod(v1, v2): return sum(v1[x]*v2[x] for x in v1.keys() & v2.keys()) This only requires that v1 and v2 both have keys and __getitem__ methods. ---------- nosy: +terry.reedy stage: -> test needed _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30196> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com