Lazy countmap

I am triaging elements in a list according to their values by some function f and compute the number of elements for each possible value as follows.

countmap(map(f, list))

This returns a dict whose keys are unique values of f and values are the number of occurrence of elements in the list with these values.

I would like to make a lazy version of the preceding code, like

mapcount(f(x) for x in gen)

but mapcount does not take a generator as argument. Is there a lazy version of mapcount in some library? I have looked at Iterators and Itertools but I don’t think I saw it.

Arguably a lazy mapcount is not very hard to implement but this is a basic task so I guess it must be somewhere. Maybe in some statistical package a la R which uses lazy evaluation? StatsBase seems to be strict.

AFAICT accumulators in DataStructures support generic iterables. Combine with IterTools.imap.

OnlineStats also has a CountMap that can be fit by iterating once through the data.

2 Likes

Thanks for the answer. I finally went with DataStructures.counter.

   counter(f(x) for x in gen).map