I have a function 'f' and a list 'l'. I want a dictionary where the keys
are evaluations of 'f(thing from l)' and the values are lists of stuff
from 'l' that matches. So for instance, if 'f = lambda x: x%3' and
'l=range(9)', then I want { 0: [0,3,6], 1:[1,4,7], 2:[2,5,8]}.

I can do that with an explicit loop, or with this suggestion (untested)

    d = {}
    def appender(e):
        d.get(f(e), []).append(e)
    map(appender, l)

but I was wondering if there's some.... prettier way to do it with some
fancy dictionary comprehension or itertools expression or something. At
this point I'm asking mostly out of curiosity and less for some code to
actually use, so either Python 2 or 3 is acceptable (but I'm writing in
3 at the moment).

Evan


Attachment: signature.asc
Description: OpenPGP digital signature

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to