
Guten Abend, ich habe ein Dictionary mit Dictionarys als Elemente die wiederrum Listen als Elemente enthalten. Das ganze sieht etwa so aus: Dictionary container: {'eins': {'martin': [1, 2, 3]}, 'zwei': {'heinz': [4, 5, 6]}} wenn ich jetzt container.copy() aufrufe wird von Dictionary container eine flache Kopie erzeugt. Die Dictionarys und Listen im Dictionary bleiben davon unberührt, sie zeigen nach wie vor auf das Original. Hat das neue Dictionary zum Beispiel den Namen container1 dann würden sich die Daten von "martin" ändern wenn man "martin" im container (Original) ändert. Gibt es ein Trick den man Anwenden kann, der da zuführt das alle anderen Dictionarys und Listen gleich mit kopiert werden? Oder muß ich alle Dictionarys und Listen einzeln ansprechen. Schon mal Danke im voraus. Mit freundlichen Grüßen Albert _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de

On Monday 21 February 2005 00:57, Albert Hermeling wrote:
ich habe ein Dictionary mit Dictionarys als Elemente die wiederrum Listen als Elemente enthalten. Das ganze sieht etwa so aus:
Dictionary container: {'eins': {'martin': [1, 2, 3]}, 'zwei': {'heinz': [4, 5, 6]}}
wenn ich jetzt container.copy() aufrufe wird von Dictionary container eine flache Kopie erzeugt. Die Dictionarys und Listen im Dictionary bleiben davon unberührt, sie zeigen nach wie vor auf das Original.
Hat das neue Dictionary zum Beispiel den Namen container1 dann würden sich die Daten von "martin" ändern wenn man "martin" im container (Original) ändert.
Gibt es ein Trick den man Anwenden kann, der da zuführt das alle anderen Dictionarys und Listen gleich mit kopiert werden? Oder muß ich alle Dictionarys und Listen einzeln ansprechen.
Ok, mal sehen: Python 2.3.5 (#2, Feb 9 2005, 00:38:15) [GCC 3.3.5 (Debian 1:3.3.5-8)] on linux2
a = {'eins': {'martin': [1, 2, 3]}, 'zwei': {'heinz': [4, 5, 6]}} b = a.copy() b['eins']['martin'][0] = 2 a {'eins': {'martin': [2, 2, 3]}, 'zwei': {'heinz': [4, 5, 6]}}
Tjo, flache Kopie.
from copy import copy b = copy(a) b['eins']['martin'][0] = 3 a {'eins': {'martin': [3, 2, 3]}, 'zwei': {'heinz': [4, 5, 6]}}
Hmm, immer noch flach.
from copy import deepcopy b = deepcopy(a) b['eins']['martin'][0] = 4 a {'eins': {'martin': [3, 2, 3]}, 'zwei': {'heinz': [4, 5, 6]}}
Ha, jetzt gehts :). Was denn Unterschied angeht, hier mal ein Zitat aus dem docstring: : x = copy.copy(y) # make a shallow copy of y : x = copy.deepcopy(y) # make a deep copy of y : : The difference between shallow and deep copying is only relevant for : compound objects (objects that contain other objects, like lists or : class instances). : : - A shallow copy constructs a new compound object and then (to the : extent possible) inserts *the same objects* into in that the : original contains. : : - A deep copy constructs a new compound object and then, recursively, : inserts *copies* into it of the objects found in the original. Gruss, Jochen _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de

Am Montag, 21. Februar 2005 01:29 schrieb Jochen Wersdörfer:
On Monday 21 February 2005 00:57, Albert Hermeling wrote:
Guten Morgen Jochen,
ich habe ein Dictionary mit Dictionarys als Elemente die wiederrum Listen als Elemente enthalten. Das ganze sieht etwa so aus:
Dictionary container: {'eins': {'martin': [1, 2, 3]}, 'zwei': {'heinz': [4, 5, 6]}}
from copy import deepcopy b = deepcopy(a)
danke für die schnelle Antwort, ich wußte gar nicht das es ein Modul copy gibt. Das löst alle meine Probleme :-)). mfg Albert PS Hast Du die Mail wirklich um 1:29 geschrieben? Gehen Python Programmierer eigentlich nie schlafen? ;-)) _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de

Hallo, Am Montag, 21. Feb 2005, 00:57:49 +0100 schrieb Albert Hermeling:
ich habe ein Dictionary mit Dictionarys als Elemente die wiederrum Listen als Elemente enthalten. Das ganze sieht etwa so aus:
Dictionary container: {'eins': {'martin': [1, 2, 3]}, 'zwei': {'heinz': [4, 5, 6]}}
wenn ich jetzt container.copy() aufrufe wird von Dictionary container eine flache Kopie erzeugt. Die Dictionarys und Listen im Dictionary bleiben davon unberührt, sie zeigen nach wie vor auf das Original.
In etwa so (ungetestet): def deepcopy( obj): class C: pass t = type( obj) if t == type( {}): r = {} for k in obj.keys(): r[ k] = deepcopy( obj[ k]) return r elif t == type( C()): r = C() r.__class__ = obj.__class__ r.__dict__ = deepcopy( obj.__dict__) else: return obj.copy() Ich empfehle, an die Klassenobjekte gleich mitzudenken. Bertram -- Bertram Scharpf Stuttgart, Deutschland/Germany http://www.bertram-scharpf.de _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de

Hallo, Am Montag, 21. Feb 2005, 10:04:40 +0100 schrieb Bertram Scharpf:
Am Montag, 21. Feb 2005, 00:57:49 +0100 schrieb Albert Hermeling:
In etwa so (ungetestet):
def deepcopy( obj): class C: pass t = type( obj) if t == type( {}): r = {} for k in obj.keys(): r[ k] = deepcopy( obj[ k]) return r elif t == type( C()): r = C() r.__class__ = obj.__class__ r.__dict__ = deepcopy( obj.__dict__) elif t == type( []): return [ x.deepcopy() for x in obj] elif t == type( ()): return tuple( [ x.deepcopy() for x in obj]) else: return obj.copy()
Da gehört natürlich noch die Behandlung von Arrays und von Tuples mit rein, sonst ist's ein grausiger Pfusch. Bertram -- Bertram Scharpf Stuttgart, Deutschland/Germany http://www.bertram-scharpf.de _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de

Eigene Deepcopy...
def deepcopy( obj): class C: pass
[..... usw. , usw.... ] in python24\lib\copy gibt es deepcopy ... incl. der Sondererwartungen mit allen bekannten Python Typen, getestet und mit Testcode, in Beobachtung der herausragendsten Python-Entwickler. Meine Empfehlung: das verwenden. Und: Dort sind gut 150 Zeilen für deepcopy gebraucht - kürzer wird es also nur mit VIEL mühe gehen, oder? Gruß Harald -- GHUM Harald Massa Harald Armin Massa Reinsburgstraße 202b 70197 Stuttgart 0173/9409607 _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
participants (4)
-
Albert Hermeling
-
Bertram Scharpf
-
Harald Armin Massa
-
Jochen Wersdörfer