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
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.
>
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
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
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
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
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 =
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