[Tutor] Question about Dictionaries

Huy Ton That huyslogic at gmail.com
Tue Aug 17 01:12:39 CEST 2010


What do you mean by subclass?

On Aug 16, 2010 3:26 PM, "Emile van Sebille" <emile at fenx.com> wrote:

On 8/16/2010 10:44 AM Chorn, Guillaume said...


>
> Hi All,
>
> I know that I can look up the value for a particular key in a
> dictionary, but can...
Yes.  But you'll need to implement it.  There are likely modules out there
that'll do this, but it'd take more time to look up and evaluate than to
simply write and implement exactly what you need.



> I understand that this could be problematic from the standpoint
> of multiple keys having the sa...
So, in untested code you'd have a function something like:

result = []

for ky, val in dict.items():
 if val == targetval:
 result.append(ky)
return result

If you need repeated access such that iterating over a large dict frequently
impacts performance, you could subclass dict and maintain a second index
allowing instant access to the keys associated with a specific value.

HTH,

Emile



_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100816/fca50ae4/attachment.html>


More information about the Tutor mailing list