By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,173 Members | 796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,173 IT Pros & Developers. It's quick & easy.

key - key pairs

P: n/a
Hello,
is there in python a kind of dictionary that supports key - key pairs?
I need a dictionary in which I can access a certain element using two
different keys, both unique.

For example:

I've a dictionary with strings and times. Sometimes I have the string and I
want to have the time, other time I've the time and I want the string. It
is important that one of the keys supports the min/max builtin function.

Thanks,

Florian
Jul 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
> is there in python a kind of dictionary that supports key - key pairs?
I need a dictionary in which I can access a certain element using two
different keys, both unique.
A Python dictionary needs a unique key, so a pair
of keys is still one unique key, but probably it is some
kind of misunderstanding here, because a dictionary
is not a database which needs a key to quickly find
an entry, so it can have as many keys as required.

What about two dictionaries where each has as a value
a key to the target dictionary with the actual values?
For example:

I've a dictionary with strings and times. Sometimes I have the string and I want to have the time, other time I've the time and I want the string. It
is important that one of the keys supports the min/max builtin function. From this example it seems, that what is needed is
a two-way dictionary. I don't know about a special
kind of dictionary for this, so maybe someone else
knows about such.
I can only recommend to use two dictionaries,
where the second one is created out of the first one,
so that they key, value pair is reversed.
If you need some code for the latter, let me know.

Claudio
"Florian Lindner" <Fl*************@xgm.de> schrieb im Newsbeitrag
news:d9*************@news.t-online.com... Hello,
is there in python a kind of dictionary that supports key - key pairs?
I need a dictionary in which I can access a certain element using two
different keys, both unique.

For example:

I've a dictionary with strings and times. Sometimes I have the string and I want to have the time, other time I've the time and I want the string. It
is important that one of the keys supports the min/max builtin function.

Thanks,

Florian

Jul 19 '05 #2

P: n/a
Florian Lindner wrote:
Hello,
is there in python a kind of dictionary that supports key - key pairs?
I need a dictionary in which I can access a certain element using two
different keys, both unique.

For example:

I've a dictionary with strings and times. Sometimes I have the string and I
want to have the time, other time I've the time and I want the string. It
is important that one of the keys supports the min/max builtin function.

Thanks,

Florian


As Claudio suggests, you probably want to make a class with (at least)
a pair of dictionaries. Are the strings one-to-one with the times?

I am unsure of what you mean by "supports the min/max builtin function."
If all you mean is, "can I get the min and/or max of the keys (strings
or times), then a pair of dictionaries does it. If you mean "finds
faster than linear," you need to say what operations are to be fastest,
and whether you mean worst case or amortized. It is possible to make
the fast operations most combinations of:
"find least",
"find greatest",
"remove least",
"remove greatest",
"find arbitrary",
"remove arbitrary",
"add entry"

Depending on your choice on costs, the data structure changes.

--Scott David Daniels
Sc***********@Acm.Org
Jul 19 '05 #3

P: n/a
Just an example of dictionary object in python.

PythonWin 2.4.1 (#65, Mar 30 2005, 09:33:37) [MSC v.1310 32 bit
(Intel)] on win32.
Portions Copyright 1994-2004 Mark Hammond (mh******@skippinet.com.au) -
see 'Help/About PythonWin' for further copyright information.
d = {}
d["key1"] = "value1"
d["key2"] = "value2"
d["key3"] = "value3"
d["key3"] = "value3"

for i, j in d.items(): .... print i, j
....
key3 value3
key2 value2
key1 value1 if d.has_key("key1"): .... print d["key1"]
.... else:
.... print "no key"
....
value1 if d.has_key("not in dict"): .... print d["key1"]
.... else:
.... print "no key"
....
no key


HTH

Sameer

Jul 19 '05 #4

P: n/a
On Thursday 23 June 2005 02:40 pm, Florian Lindner wrote:
is there in python a kind of dictionary that supports key - key pairs?
I need a dictionary in which I can access a certain element using two
different keys, both unique.

For example:

I've a dictionary with strings and times. Sometimes I have the string and I
want to have the time, other time I've the time and I want the string. It
is important that one of the keys supports the min/max builtin function.


Well, really, you're always using one or the other as the "key" and the other
as the "value". Furthermore, it is not in the general case assured that you
can do this --- the keys may not really be 1:1.

If you are content to restrict yourself to the 1:1 case, you can construct
an inverse dictionary from the first dictionary like this:

time2string = dict([ (b,a) for a,b in string2time.items() ])

Note that if string2time has duplicate values, this will arbitrarily pick
one (in a consistent, but implementation dependent way) to use as
the key in the inverse mapping.

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 19 '05 #5

P: n/a
Hi All--

Terry Hancock wrote:

On Thursday 23 June 2005 02:40 pm, Florian Lindner wrote:
is there in python a kind of dictionary that supports key - key pairs?
I need a dictionary in which I can access a certain element using two
different keys, both unique.

For example:

I've a dictionary with strings and times. Sometimes I have the string and I
want to have the time, other time I've the time and I want the string. It
is important that one of the keys supports the min/max builtin function.


Well, really, you're always using one or the other as the "key" and the other
as the "value". Furthermore, it is not in the general case assured that you
can do this --- the keys may not really be 1:1.

If you are content to restrict yourself to the 1:1 case, you can construct
an inverse dictionary from the first dictionary like this:

time2string = dict([ (b,a) for a,b in string2time.items() ])

Note that if string2time has duplicate values, this will arbitrarily pick
one (in a consistent, but implementation dependent way) to use as
the key in the inverse mapping.


Well, Florian said, "using two different keys, both unique"; if that is
true, then a single key maps to a single value & vice versa. Easiest
way, it seems to me, would be to subclass dict and provide get/set that
always insert the value as a key. So that dict["string"]=time also
means dict[time]="string". Only one dict required then.

Or am I missing something?

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #6

P: n/a
Ivan Van Laningham wrote:
Well, Florian said, "using two different keys, both unique"; if that is
true, then a single key maps to a single value & vice versa. Of course you are right. I got caught up in the problem I imagined (the
pair being unique).
... subclass dict and provide get/set that always insert the value
as a key. So dict["string"]=time also means dict[time]="string".
Or am I missing something?

Yup, getting to min and max. I presume that will be key-dependent.

--Scott David Daniels
Sc***********@Acm.Org
Jul 19 '05 #7

P: n/a
No need to update __getitem__, since the modified __setitem__ drops in
the reverse values. But __delitem__ needs overriding, and some special
guard needs to be added to __setitem__ to prevent orphaning any old
value:key entries.

-- Paul
Here's one possible solution:

class SymmetricDict(dict):
def __delitem__(self,x):
v = self[x]
super(SymmetricDict,self).__delitem__(x)
super(SymmetricDict,self).__delitem__(v)
def __setitem__(self,k,v):
if k in self:
del self[k]
if v in self:
del self[v]
super(SymmetricDict,self).__setitem__(k,v)
super(SymmetricDict,self).__setitem__(v,k)

sd = SymmetricDict()

sd["A"] = 1
print sd["A"]
print sd[1]
sd["A"] = 2
print sd["A"]
print sd[2]
print sd[1]

prints:

1
A
2
A
Traceback (most recent call last):
File "symmetricDict.py", line 25, in ?
print sd[1]
KeyError: 1

Jul 19 '05 #8

P: n/a
Man, this is not my week! Another bug in my posted code! The posted
version of SymmetricDict fails when adding an entry in which the key
equals the value. First bug is in __setitem__ in which the insertion
is done twice, which is wasteful but benign. The second bug is in
__delitem__, which throws an exception when we try to delete the
back-pointing entry - which was already deleted since there is only one
entry.

Another cautionary tale on the value of testing...

Here it the improved SymmetricDict code.

-- Paul
class SymmetricDict(dict):
def __delitem__(self,k):
v = self[k]
super(SymmetricDict,self).__delitem__(k)
if not v==k:
super(SymmetricDict,self).__delitem__(v)

def __setitem__(self,k,v):
if k in self:
del self[k]
if v in self:
del self[v]
super(SymmetricDict,self).__setitem__(k,v)
if not v==k:
super(SymmetricDict,self).__setitem__(v,k)

Jul 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.