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 8 2119
> 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
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
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
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
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
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
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
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Equis Uno |
last post by:
Hi,
Assume I'm given a 100k file full of key-value pairs:
date,value
26-Feb-04,36.47
25-Feb-04,36.43
24-Feb-04,36.30
23-Feb-04,37.00
20-Feb-04,37.00
|
by: Bill |
last post by:
If, for example, I retrieve a connectionstring from a config file using
something like:
Value = ConfigurationSettings.AppSettings;
This will return a string that is semi-colon delimited. If I...
|
by: He Shiming |
last post by:
Hi Folks,
Happy holidays! I have a question regarding STL multimap. Basically, the
current multimap<int,int> look like this:
key=>value
1=>10,
1=>20,
1=>30,
|
by: Evan |
last post by:
Is there a simple way to to identify and remove matching pairs from 2
lists?
For example:
I have
a=
b=
|
by: Evyn |
last post by:
Hi,
How do I compare 2 maps for identical keys, and if they have identical
keys, check if they have identical values? In either case I want to
copy ONLY that key value pair to one of two...
|
by: Arjen |
last post by:
Hi,
I have this string:
private string pairs = { "AB", "CD", "BD", "AC", "BC", "AD", "AB", "CD" };
Now I want to randomize all pairs, like the first AB to BA.
for (int i = 0; i < 8; i++)
{...
|
by: Alan Isaac |
last post by:
I want to generate sequential pairs from a list.
Here is a way::
from itertools import izip, islice
for x12 in izip(islice(x,0,None,2),islice(x,1,None,2)):
print x12
(Of course the print...
|
by: tmitt |
last post by:
Hi, I missed a couple classes due to being sick this past week and I am having trouble getting steered in the right direction for my programming assignment. I'd ask the professor for help but he has...
|
by: mcjason |
last post by:
I saw something interesting about a grid pair puzzle problem that
it looks like a machine when you find each that work out the way it
does and say with all the others that work out the way they...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |