4 ms·
It's very common in data processing to sum up stuff (e.g. unique visitors, total visitors, total revenue, etc.) To handle this you typically want to write a gen
by noelwelsh 6y ago
It's very common in data processing to sum up stuff (e.g. unique visitors, total visitors, total revenue, etc.) To handle this you typically want to write a generic library that makes summing up stuff easy. I can't think of an example where you'd want to add up strings (more likely lists of strings) but it is definitely the case for other data structures (sets, vectors, lists, numbers, etc.)
- kragen 6y agoPython sum() can handle summing up other data structures, although, as with foldl, you may have to give it an initial accumulator argument: >>> import numpy as np >>> xs = np.arange(9).reshape((3,3)) >>> xs array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> sum(xs) # builtin sum, not numpy sum array([ 9, 12, 15]) >>> sum([[3, 11], [5]], []) [3, 11, 5] >>> sum([(3, 11), (5,)], ()) (3, 11, 5) However, that doesn't work for strings; there's a special-case check for that.