[Python] dizionario: ottenere chiave da valore

Nicola Ferrari nick.ferro a gmail.com
Sab 12 Feb 2011 10:18:33 CET


:) ok grazie


Il giorno 12 febbraio 2011 10:16, Enrico Franchi
<enrico.franchi a gmail.com>ha scritto:

> Nicola Ferrari wrote:
>
>> ho un dizionario di n elementi.. per prendere il valore massimo faccio
>> max(dizionario.values()).. esiste un metodo che dato un valore mi
>> recupera la chiave di questo valore?
>>
>
> Non un metodo efficiente. Un dizionario e' fatto per recuperare
> efficientemente il valore data una chiave, non viceversa.
> Quello che io farei e' ottenerlo automaticamente durante la scansione
> comunque lineare del dizionario che fai per values.
>
> In [1]: d =dict(a=10, b=1000, c=10000)
>
> In [2]: max?
> Type:           builtin_function_or_method
> Base Class:     <type 'builtin_function_or_method'>
> String Form:    <built-in function max>
> Namespace:      Python builtin
> Docstring:
>    max(iterable[, key=func]) -> value
>    max(a, b, c, ...[, key=func]) -> value
>
>    With a single iterable argument, return its largest item.
>    With two or more arguments, return the largest argument.
>
> <osserva il parametro key>
>
>
> In [3]: import operator as op
> In [4]: max(d.iteritems(), key=op.itemgetter(1))
> Out[4]: ('c', 10000)
>
> Eccoti li il massimo secondo il valore, con bello e pronto anche la sua
> chiave.
>
> --
> .
> ..: -enrico-
>
> _______________________________________________
> Python mailing list
> Python a lists.python.it
> http://lists.python.it/mailman/listinfo/python
>



-- 
Nicola Ferrari
website: http://www.nicolaferrari.name

skype: nick.ferro
-------------- parte successiva --------------
Un allegato HTML è stato rimosso...
URL: <http://lists.python.it/pipermail/python/attachments/20110212/7f6c6b60/attachment.html>


Maggiori informazioni sulla lista Python