On May 3, 5:12 pm, dave <[EMAIL PROTECTED]> wrote:
> thanks Duncan and Arnaud.
>
> I'm learning Python from the "How to Think Like a Python Programmer"
> book by Allen Downey.  My first try used the "inv[val] = [key]" and
> then the next problem was to incorporate the "D.setdefault(...)" method.
>
> Thank you for your help.  I'm always amazed how kind people are in this group.
>
> On 2008-05-03 14:57:29 -0600, Arnaud Delobelle <[EMAIL PROTECTED]> said:
>
> > dave <[EMAIL PROTECTED]> writes:
>
> >> Hello,
>
> >> here is a piece of code I wrote to check the frequency of values and
> >> switch them around to keys in a new dictionary.  Just to measure how
> >> many times a certain key occurs:
>
> >> def invert(d):
> >>        inv = {}
> >>        for key in d:
> >>                val = d[key]
> >>                if val not in inv:
> >>                        inv.setdefault(val, [key])
> > You can simply write:
> >                         inv[val] = [key]
> >>                else:
> >>                        inv[val].append(key)
> >>        return inv
>
> >> Using the methods above (I'm just a beginner) could I have written it
> >> more concisely?  Any criticism/critique in the code would be greatly
> >> appreciated.
>
> > Apart from the unnecessary use of setdefault, it looks good to me.
>
> > * You could change if 'val not in inv:' to 'if val in inv:' (and swap
> >   the if and else clauses of course) in order to have a positive
> >   condition rather than a negative one
>
> > * If you want to use setdefault, you can replace the if .. else
> >   construct by:
>
> >                inv.setdefault(val, []).append(key)
>
> > * You can also iterate over keys and values using the items() or
> >   iteritems() method of dictionaries:
>
> > def invert(d):
> >     inv = {}
> >     for key, val in d.iteritems():
> >         inv.setdefault(val, []).append(key)
> >     return inv
>
>

In Python 2.5 and later you may use the defaultdict class which is
faster and slightly more elegant in such cases:

from collections import defaultdict

def invert(d):
    inv = defaultdict(list)
    for key, val in d.iteritems():
        inv[val].append(key)
    return inv

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

Reply via email to