Re: [dictionary] how to get key by item

2004-12-14 Thread Skip Montanaro
Ola> If some keys has the same value as the item this will cause Ola> problems because keys in your result dictionary can be Ola> overwritten. That's why I said, "assuming your dictionary defines a one-to-one mapping...". Skip -- http://mail.python.org/mailman/listinfo/python-list

RE: [dictionary] how to get key by item

2004-12-14 Thread Delaney, Timothy C (Timothy)
Roy Smith wrote: >> >>> forward = {10 : 50, 2 : 12, 4 : 43} >> >>> reverse = dict([(v,k) for (k,v) in forward.iteritems()]) >> >>> print forward {10: 50, 4: 43, 2: 12} >> >>> print reverse >> {50: 10, 43: 4, 12: 2} > > BTW, does Python really build the intermediate list an

RE: [dictionary] how to get key by item

2004-12-14 Thread Skip Montanaro
Tim> Python will do what you tell it. In the above case, it will build a Tim> list. Whoops, yeah. I called .iteritems() then forgot to use a generator expression... Skip -- http://mail.python.org/mailman/listinfo/python-list

Re: [dictionary] how to get key by item

2004-12-14 Thread Roy Smith
"Delaney, Timothy C (Timothy)" <[EMAIL PROTECTED]> wrote: > Python will do what you tell it. Certainly it does. The problem is that sometimes what I told it to do and what I think I told it to do are two different things :-) > Using Python 2.4, the above can be rewritten as a generator expressi

Re: [dictionary] how to get key by item

2004-12-14 Thread Roy Smith
In article <[EMAIL PROTECTED]>, Skip Montanaro <[EMAIL PROTECTED]> wrote: > Egor> i know how to get item by key > ... > Egor> but i wonder how to get key by item > > Assuming your dictionary defines a one-to-one mapping, just invert it: > > >>> forward = {10 : 50, 2 : 12, 4 : 43

Re: [dictionary] how to get key by item

2004-12-14 Thread Fredrik Lundh
Skip Montanaro wrote: > That doubles your storage careful: it creates another dictionary structure with the same size as the first one, but it doesn't copy the objects in the dictionary. so whether it doubles the actual memory usage depends on what data you have in the dictionary (last time I ch

Re: [dictionary] how to get key by item

2004-12-13 Thread Skip Montanaro
Egor> i know how to get item by key ... Egor> but i wonder how to get key by item Assuming your dictionary defines a one-to-one mapping, just invert it: >>> forward = {10 : 50, 2 : 12, 4 : 43} >>> reverse = dict([(v,k) for (k,v) in forward.iteritems()]) >>> print forward

[dictionary] how to get key by item

2004-12-13 Thread Egor Bolonev
saluton al ciuj i know how to get item by key == dict = {10 : 50, 2 : 12, 4 : 43} print dict[2] 12 but i wonder how to get key by item print dict[12] 2 == is there a more fast way than that one (my dictionary is really big) == dict = {10 : 50, 2 : 12,

Re: [dictionary] how to get key by item

2004-12-13 Thread Skip Montanaro
Fredrik> Skip Montanaro wrote: >> That doubles your storage Fredrik> careful: it creates another dictionary structure with the same Fredrik> size as the first one, but it doesn't copy the objects in the Fredrik> dictionary. Yes, sorry. The OP indicated the original dictionar

Re: [dictionary] how to get key by item

2004-12-13 Thread Nick Coghlan
Ola Natvig wrote: If some keys has the same value as the item this will cause problems because keys in your result dictionary can be overwritten. Could it be a option to build the result dictionary as a dictionary with the values as the keys, and lists of keys as the value. Perhaps you need to

Re: [dictionary] how to get key by item

2004-12-13 Thread Ola Natvig
Skip Montanaro wrote: Egor> i know how to get item by key ... Egor> but i wonder how to get key by item Assuming your dictionary defines a one-to-one mapping, just invert it: >>> forward = {10 : 50, 2 : 12, 4 : 43} >>> reverse = dict([(v,k) for (k,v) in forward.iteritems()])

Re: [dictionary] how to get key by item

2004-12-13 Thread Keith Dart
Skip Montanaro wrote: Egor> i know how to get item by key ... Egor> but i wonder how to get key by item Assuming your dictionary defines a one-to-one mapping, just invert it: >>> forward = {10 : 50, 2 : 12, 4 : 43} >>> reverse = dict([(v,k) for (k,v) in forward.iteritems()])

Re: [dictionary] how to get key by item

2004-12-13 Thread Aahz
In article <[EMAIL PROTECTED]>, Skip Montanaro <[EMAIL PROTECTED]> wrote: > >Assuming your dictionary defines a one-to-one mapping, just invert it: > >>>> forward = {10 : 50, 2 : 12, 4 : 43} >>>> reverse = dict([(v,k) for (k,v) in forward.iteritems()]) >>>> print forward >{10: 50,

Re: [dictionary] how to get key by item

2004-12-13 Thread Roy Smith
Skip Montanaro <[EMAIL PROTECTED]> wrote: > Roy> BTW, does Python really build the intermediate list and throw it > Roy> away after using it to initialize the dictionary, or is it smart > Roy> enough to know that it doesn't really need to build the whole list > Roy> in memory? > >

Re: [dictionary] how to get key by item

2004-12-13 Thread Skip Montanaro
>> That doubles your storage, so you'll have to trade that off against >> the speed gain of not having to loop over the entire dictionary. Roy> Well, you *do* loop over the entire dictionary, but you only do it Roy> once, when you create the reverse dict. If you are only going to