Re: dict invert - learning question

2008-05-03 Thread Jerry Hill
On Sat, May 3, 2008 at 4:29 PM, dave <[EMAIL PROTECTED]> wrote: > 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

Re: dict invert - learning question

2008-05-03 Thread George Sakkis
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. >

Re: dict invert - learning question

2008-05-03 Thread mrkafk
On 4 Maj, 01:27, [EMAIL PROTECTED] wrote: > >>> a={1:'a', 2:'b', 3:'c'} Oops, it should obviously be: >>> dict(zip(a.values(), a.keys())) {'a': 1, 'c': 3, 'b': 2} -- http://mail.python.org/mailman/listinfo/python-list

Re: dict invert - learning question

2008-05-03 Thread mrkafk
Assuming all the values are unique: >>> a={1:'a', 2:'b', 3:'c'} >>> dict(zip(a.keys(), a.values())) {1: 'a', 2: 'b', 3: 'c'} The problem is you obviously can't assume that in most cases. Still, zip() is very useful function. -- http://mail.python.org/mailman/listinfo/python-list

Re: dict invert - learning question

2008-05-03 Thread dave
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 peop

Re: dict invert - learning question

2008-05-03 Thread Arnaud Delobelle
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

Re: dict invert - learning question

2008-05-03 Thread Duncan Booth
dave <[EMAIL PROTECTED]> wrote: > 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 =

dict invert - learning question

2008-05-03 Thread dave
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 in