[Baypiggies] How to create a dict of counts using functional programming
Braun Brelin
bbrelin at gmail.com
Mon Sep 28 13:01:35 CEST 2015
Hello all,
I am trying to do the following:
I have a list that looks like this:
['1','2','7','8','8']
I want to create a dictionary that looks like this:
{1:1, 2:1,7:1,8:2}
i.e. the value is the count of the number of times the key appears in
the list. The catch is that I'm trying to do this in a functional
programming way, rather than iteratively.
I'm trying to use something like map, or reduce or even dict, but I can't
figure out how to tell
python to increment the value. In other words how do I even specify what
the value is, as it
seems to be a bit of a chicken and egg scenario.
I can, for example create a dictionary that sets the values to a constant,
such as zero using FP,
but how do I tell it to increment the value for a dictionary that doesn't
yet exist as I'm in the process of
building it?
Thanks,
Braun Brelin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20150928/b151e1b8/attachment.html>
More information about the Baypiggies
mailing list