473,385 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

dictionaries/pointers

I dont know how to do this and can't think of a simple way to.

All I want is a dictionary where two keys point to the same object.
(to steal the ascii art from
http://starship.python.net/crew/mwh/...jectthink.html)
I want sometihng like this:

,------. +-------+
| dict |------>|+-----+| +---+
`------' || "a" |+---->| 1 |
|+-----+| +---+
| | ^
|+-----+| |
|| "b" |+-------'
|+-----+|
+-------+
| |
|+-----+| +---+
|| "c" |+---->| 2 |
|+-----+| +---+
+-------+

Where if I change "a" or "b" to 3 the other one will change?
Is this even possible? How would I do it?

Oct 7 '05 #1
2 1282
Rob Conner wrote:
I dont know how to do this and can't think of a simple way to.

All I want is a dictionary where two keys point to the same object.
(to steal the ascii art from
http://starship.python.net/crew/mwh/...jectthink.html)
I want sometihng like this:

,------. +-------+
| dict |------>|+-----+| +---+
`------' || "a" |+---->| 1 |
|+-----+| +---+
| | ^
|+-----+| |
|| "b" |+-------'
|+-----+|
+-------+
| |
|+-----+| +---+
|| "c" |+---->| 2 |
|+-----+| +---+
+-------+

Where if I change "a" or "b" to 3 the other one will change?
Is this even possible? How would I do it?


Objects of type int are immutable in Python, so you'll need a helper
class to do this. Try something like:
class Container: .... def __init__(self, value=None): self.value = value
.... def get(self): return self.value
.... def set(self, value): self.value = value
.... one = Container(1)
myDictionary = {}
myDictionary['a'] = one
myDictionary['b'] = one
myDictionary['b'].set(3)
print myDictionary['a'].get()

3

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Never make a promise or plan / Take a little love where you can
-- Florence, _Chess_
Oct 7 '05 #2
On 7 Oct 2005 14:23:49 -0700, "Rob Conner" <rt******@gmail.com> wrote:
I dont know how to do this and can't think of a simple way to.

All I want is a dictionary where two keys point to the same object.
(to steal the ascii art from
http://starship.python.net/crew/mwh/...jectthink.html)
I want sometihng like this:

,------. +-------+
| dict |------>|+-----+| +---+
`------' || "a" |+---->| 1 |
|+-----+| +---+
| | ^
|+-----+| |
|| "b" |+-------'
|+-----+|
+-------+
| |
|+-----+| +---+
|| "c" |+---->| 2 |
|+-----+| +---+
+-------+

Where if I change "a" or "b" to 3 the other one will change?
Is this even possible? How would I do it?


A simple, ugly answer: Use a mutable object rather than a plain
integer. Example:
elt = [1]
dict = {"a":elt, "b":elt, "c":[2]}
print dict {'a': [1], 'c': [2], 'b': [1]} dict["a"][0] = 3
print dict {'a': [3], 'c': [2], 'b': [3]}


Regards,

-=Dave
--
Change is inevitable, progress is not.
Oct 7 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Kerry Neilson | last post by:
Hi, Really hung up on this one. I'm trying to get all the fields of a dictionary to be unique for each class: class A { my_dict = dict_entry = { 'key1':0, 'key2':0 } __init__(self): for...
0
by: Till Plewe | last post by:
Is there a way to speed up killing python from within a python program? Sometimes shutting down takes more than 10 times as much time as the actual running of the program. The programs are...
8
by: Frohnhofer, James | last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a function. I did not want to do def fn(): a = {} b = {} c = {} . . . z = {}
3
by: Shivram U | last post by:
Hi, I want to store dictionaries on disk. I had a look at a few modules like bsddb, shelve etc. However would it be possible for me to do the following hash = where the key is an int and not...
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
16
by: Ben | last post by:
I'm trying to make myself a dictionary for tsearch2 that converts numbers to their english word equivalents. This seems to be working great, except that I can't figure out how to make my lexize...
2
by: David Pratt | last post by:
Hi. I like working with lists of dictionaries since order is preserved in a list when I want order and the dictionaries make it explicit what I have got inside them. I find this combination very...
8
by: placid | last post by:
Hi all, Just wondering if anyone knows how to pop up the dialog that windows pops up when copying/moving/deleting files from one directory to another, in python ? Cheers
1
by: Edwin.Madari | last post by:
by the way, iterating over bar will throw KeyError if that key does not exist in foo. to see that in action, simply set another key in bar after copy.deepcopy stmt in this example.. bar = 0 and...
14
by: cnb | last post by:
Are dictionaries the same as hashtables?
1
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...
0
isladogs
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...
0
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,...
0
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$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.