473,320 Members | 2,122 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,320 software developers and data experts.

Working with copies of a dictionary

Hi!

Damnit, am I angry! Please look at the following example:

class bla:
def __init__(self):
self.ho = {'a': ['b'], 'c': ['b', 'e', 'f', 'd'], 'b':
['a', 'e', 'f', 'c'], 'e': ['b', 'c', 'g', 'h'], 'd': ['c'], 'g': ['e',
'h'], 'f': ['b', 'c'], 'h': ['e', 'g']} # shall be an adjacence list of
a graph...
self.oha = [('a', 'b'), ('b', 'c'), ('b', 'e'), ('b', 'f'),
('c', 'd'), ('c', 'e'), ('c', 'f'), ('e', 'g'), ('e', 'h'), ('g', 'h')]
def do_stuff(self):
next = self.ho.copy() # HERE!!!

edges = self.oha[:]

next['c'].remove('e')
next['g'].remove('h')
del next['b']
edges.pop(0)
edges.pop(3)
edges.pop(6)

f = bla()
print f.ho
print f.oha

OK, so the two last lines, what surprising thing give me exactly the
long dictionary I defined in __init__.

f.do_stuff()

Now you see that I do nothing with the self.* thingies themselves. So
why in hell do the following two lines give me a different output???

print f.ho
print f.oha

f.oha hasn't changed, but f.ho now lacks the two elements I deleted
from next with the remove() call, while the 'b' array is still there.
Now can anyone please explain me why do_stuff() ha changed the
attributes of the class?? Even the following:

next = {}
for v, e in self.ho.items():
next[v] = e

, where one can easily see that next does not even have the slightest
connection to f.ho, changes it!! I am completely desparate, because I
would just like to work with the copy of that dictionary, without
deleting the proper one. How can I do such a thing?

Thanks in Advance
Tobias

--
please send any mail to botedesschattens(at)web(dot)de
Jul 18 '05 #1
2 1651

"Tobias Pfeiffer" <me@privacy.net> wrote in message
news:br************@ID-162581.news.uni-berlin.de...
Hi!

Damnit, am I angry! Please look at the following example:

class bla:
def __init__(self):
self.ho = {'a': ['b'], 'c': ['b', 'e', 'f', 'd'], 'b':
['a', 'e', 'f', 'c'], 'e': ['b', 'c', 'g', 'h'], 'd': ['c'], 'g': ['e',
'h'], 'f': ['b', 'c'], 'h': ['e', 'g']} # shall be an adjacence list of
a graph...
self.oha = [('a', 'b'), ('b', 'c'), ('b', 'e'), ('b', 'f'),
('c', 'd'), ('c', 'e'), ('c', 'f'), ('e', 'g'), ('e', 'h'), ('g', 'h')]
def do_stuff(self):
next = self.ho.copy() # HERE!!!

edges = self.oha[:]

next['c'].remove('e')
next['g'].remove('h')
del next['b']
edges.pop(0)
edges.pop(3)
edges.pop(6)

f = bla()
print f.ho
print f.oha

OK, so the two last lines, what surprising thing give me exactly the
long dictionary I defined in __init__.

f.do_stuff()

Now you see that I do nothing with the self.* thingies themselves. So
why in hell do the following two lines give me a different output???

print f.ho
print f.oha

f.oha hasn't changed, but f.ho now lacks the two elements I deleted
from next with the remove() call, while the 'b' array is still there.
Now can anyone please explain me why do_stuff() ha changed the
attributes of the class?? Even the following:

next = {}
for v, e in self.ho.items():
next[v] = e

, where one can easily see that next does not even have the slightest
connection to f.ho, changes it!! I am completely desparate, because I
would just like to work with the copy of that dictionary, without
deleting the proper one. How can I do such a thing?

Thanks in Advance
Tobias

--
please send any mail to botedesschattens(at)web(dot)de


Not even the slightest connection is not quite right.
adict = {'a':[1,2,3], 'b':[3,4,5]}
anotherdict = adict.copy()
id(adict['a']) 10410608 id(anotherdict['a']) 10410608
adict['a'] and anotherdict['a'] reference the same list. Try a deep copy.
import copy
a_third_dict = copy.deepcopy(adict)
a_third_dict {'a': [1, 2, 3], 'b': [3, 4, 5]} id(adict3['a'])

10411984

Duncan
Jul 18 '05 #2
[snip]

Not even the slightest connection is not quite right.
adict = {'a':[1,2,3], 'b':[3,4,5]}
anotherdict = adict.copy()
id(adict['a']) 10410608 id(anotherdict['a']) 10410608
adict['a'] and anotherdict['a'] reference the same list. Try a deep copy.
import copy
a_third_dict = copy.deepcopy(adict)
a_third_dict {'a': [1, 2, 3], 'b': [3, 4, 5]} id(adict3['a']) 10411984

Duncan


Of course I shouldn't edit output once I've pasted it. That should be,
id(a_third_dict['a'])

10411984
Jul 18 '05 #3

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

Similar topics

4
by: earwicker | last post by:
I recently deployed a web application which contains a user registration form with the usual fields: name, address, email, password, etc. Each of the TextBoxes uses a validation control to verify...
10
by: erin.sebastian | last post by:
Hello Everyone, I have code that uploads files in asp. It seems to be working however on files > 200kb it bombs and i don't know why. Does anyone have any idea of why this would occur and what i...
0
by: serkan | last post by:
Guys, I am trying to get this password reset functionality wor for me but I am not successful at all. Please somebody help me. I get "Your password could not be reset - please try again later" so I...
5
by: Joel Barsotti | last post by:
I'm trying to get sql cache dependency working correctly. I've got sql server 2005 for my back end. I've don the "ALTER DATABASE dbName SET broker_enable" command. I made sure the user in my...
5
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
3
by: Girish Sahani | last post by:
hi ppl, Here is a simple function to remove those keys of a dictionary whose values are less than some specified value. But it isnt working. Please help. def prune(d,cp): l = for rule,value...
5
by: Defected | last post by:
Hi All, I have problem whit this program, because the output file print copies but only at the end of file, and the file does not have two copies of the same element. Example of elements in...
3
by: mk | last post by:
Hello everyone, I'm storing functions in a dictionary (this is basically for cooking up my own fancy schmancy callback scheme, mainly for learning purpose): .... return "f2 " + arg .......
0
by: Calvin Spealman | last post by:
On Thu, Jul 17, 2008 at 7:45 AM, mk <mrkafk@gmail.comwrote: As was pointed out already, this is a basic misunderstanding of assignment, which is common with people learning Python. To your...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.