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
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
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
"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
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
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
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
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,
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
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
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()])
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()])
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,
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?
>
>
>> 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
15 matches
Mail list logo