Re: [Tutor] Question about Dictionaries

2010-08-16 Thread Steven D'Aprano
On Tue, 17 Aug 2010 09:12:39 am Huy Ton That wrote:
> What do you mean by subclass?

It's a fundamental term from object-oriented programming.

If you have a class that defines certain data and behaviour, you can 
create a *subclass* that inherits the same data and behaviour, except 
for specific examples which are over-ridden or over-loaded.

A simple example:

class MyInt(int):  # creates a subclass of int
def __str__(self):
return "My Integer %d" % self


MyInts are exactly the same as built-in ints except their string form is 
different.

>>> n = MyInt(42)
>>> print n
My Integer 42


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about Dictionaries

2010-08-16 Thread Emile van Sebille

On 8/16/2010 4:12 PM Huy Ton That said...

What do you mean by subclass?





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




Something along these lines:

class myDict(dict):
def __init__(self):
self.altKeys = {}
def __setitem__(self,ky,val):
self.altKeys[val]=ky
return dict.__setitem__(self, ky,val)
def lookup(self,ky):
return self.altKeys[ky]


a = myDict()

a[1] = 111
a[2] = 222
a[3] = 333

a[3]

a.lookup(333)


Emile

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about Dictionaries

2010-08-16 Thread Steven D'Aprano
On Tue, 17 Aug 2010 03:44:33 am Chorn, Guillaume wrote:
> Hi All,
>
> I know that I can look up the value for a particular key in a
> dictionary, but can I look up the key associated with a particular
> value?  I understand that this could be problematic from the
> standpoint of multiple keys having the same value, but even then I
> feel like Python could just return a list of keys with that value.

There is no built-in way of doing so, but it's easy to write your own.

def reverse_lookup(d, target, first_only=False):
found = []
for key, value in d.items():
if value == target:
if first_only: return key
found.append(key)
return found


Or if you want a one-liner, use a list-comp:

[k for (k,v) in d.items() if v == target]


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about Dictionaries

2010-08-16 Thread Chorn, Guillaume
Thanks Dave.  Actually I figured out a relatively simple solution to my
specific problem--since the dictionary I made was the result of zipping
together two lists, I simply made a second dictionary with the lists
switched around after the zip command and used that.  It worked in my
case because all key-value pairings were unique... 

-Original Message-
From: Dave Angel [mailto:da...@ieee.org] 
Sent: Monday, August 16, 2010 4:03 PM
To: Chorn, Guillaume
Cc: tutor@python.org
Subject: Re: [Tutor] Question about Dictionaries

Chorn, Guillaume wrote:
> Hi All,
>
> I know that I can look up the value for a particular key in a
> dictionary, but can I look up the key associated with a particular
> value?  I understand that this could be problematic from the
standpoint
> of multiple keys having the same value, but even then I feel like
Python
> could just return a list of keys with that value.
>
> Thanks!
>
> Guillaume  
>
> Notice:  This e-mail message, together with any attachments, contains
> information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
> New Jersey, USA 08889), and/or its affiliates Direct contact
information
> for affiliates is available at 
> http://www.merck.com/contact/contacts.html) that may be confidential,
> proprietary copyrighted and/or legally privileged. It is intended
solely
> for the use of the individual or entity named on this message. If you
are
> not the intended recipient, and have received this message in error,
> please notify us immediately by reply e-mail and then delete it from 
> your system.
>
>   
As long as you don't mind it being (relatively) slow, you could do 
something like (untested):

[key for key, item in mydict if item == value]

DaveA


Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates Direct contact information
for affiliates is available at 
http://www.merck.com/contact/contacts.html) that may be confidential,
proprietary copyrighted and/or legally privileged. It is intended solely
for the use of the individual or entity named on this message. If you are
not the intended recipient, and have received this message in error,
please notify us immediately by reply e-mail and then delete it from 
your system.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about Dictionaries

2010-08-16 Thread Alan Gauld

"Chorn, Guillaume"  wrote


dictionary, but can I look up the key associated with a particular
value?  


Not directly but its not hard to do bearing in mind you will 
get a collection back.:



d = {1:10,2:20,3:10,4:20}
val = 10
ks = [k for k in d if d[k] == val]
ks

[1, 3]




You need to trawl the dictionary whatever you do
(unless you get very sophisticated and build a double 
dictionary class, but then insertions get slow because 
you have to update both versions...)


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about Dictionaries

2010-08-16 Thread Huy Ton That
What do you mean by subclass?

On Aug 16, 2010 3:26 PM, "Emile van Sebille"  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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about Dictionaries

2010-08-16 Thread Dave Angel

Chorn, Guillaume wrote:

Hi All,

I know that I can look up the value for a particular key in a
dictionary, but can I look up the key associated with a particular
value?  I understand that this could be problematic from the standpoint
of multiple keys having the same value, but even then I feel like Python
could just return a list of keys with that value.

Thanks!

Guillaume  


Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station,
New Jersey, USA 08889), and/or its affiliates Direct contact information
for affiliates is available at 
http://www.merck.com/contact/contacts.html) that may be confidential,

proprietary copyrighted and/or legally privileged. It is intended solely
for the use of the individual or entity named on this message. If you are
not the intended recipient, and have received this message in error,
please notify us immediately by reply e-mail and then delete it from 
your system.


  
As long as you don't mind it being (relatively) slow, you could do 
something like (untested):


[key for key, item in mydict if item == value]

DaveA


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about Dictionaries

2010-08-16 Thread Lee Harr

> I know that I can look up the value for a particular key in a
> dictionary, but can I look up the key associated with a particular
> value?


I am using bidict in one of my projects:
http://pypi.python.org/pypi/bidict/0.1.1

It's probably a bit more complex than what I
need, but the parts I am using are working fine,
and the design of the extra features is kind of
interesting.

I think something like this should be in
the stdlib, but there is some controversy
about how exactly it should work.

  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about Dictionaries

2010-08-16 Thread Emile van Sebille

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 I look up the key associated with a particular
value?


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 same value, but even then I feel like Python
could just return a list of keys with that value.



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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor