copy.deepcopy ( )
DESCRIPTION.
Retourne une copie récursive d'un objet.
SYNTAXE.
Destination = copy.deepcopy ( Source )
Destination = ⇨ variable qui recevra la copie [ OBLIGATOIRE ]
copy.deepcopy ( ) ⇨ appel de la méthode [ OBLIGATOIRE ]
Source ⇨ objet à copier [ OBLIGATOIRE ]
REMARQUES.
Une copie récursive obtenue avec la méthode copy.récursive ( ) est distincte de son ancêtre, et tous les éléments séquences, tel que des list ( ) ou des tuples ( ), de l'objet ancêtre sont aussi distincts dans les deux objets. Toute modification d'un élément séquence dans l'objet copié ne modifie pas son homologue dans la copie et inversement.
Pour conserver les liens entre les éléments séquences des deux objets, on utilise la méthode copy.copy ( ).
EXEMPLE.
import copy
LST_Initial = [ 1 , 2 , 3 , "A" , "B" , "C" , [ 11 , 22 , 33 , "abc" ] , True , False , None ]
LST_Affectation = LST_Initial
LST_Tranche = LST_Initial [ : ]
LST_Copie = LST_Initial.copy ( )
LST_Comprehension = [ kelement for kelement in LST_Initial ]
LST_Superficiel = copy.copy ( LST_Initial )
LST_Profond = copy.deepcopy ( LST_Initial )
print ( f"Liste initiale : { LST_Initial }" )
LST_Initial.pop ( )
LST_Initial.append ( "ajouté" )
LST_Initial [ 2 ] = "modifié"
LST_Initial.insert ( 5 , "inséré" )
LST_Initial [ 7 ] [ 2 ] = "trente trois"
print ( f"Liste modifiée : { LST_Initial } \n" )
print ( "Valeur des différentes copies de la liste initiale : \n" )
print ( f"LST_Affectation = LST_Initial:\n{ LST_Affectation } \n" )
print ( f"LST_Tranche = LST_Initial [ : ] :\n{ LST_Tranche } \n" )
print ( f"LST_Copie = LST_Initial.copy ( ) :\n{ LST_Copie } \n" )
print ( f"LST_Comprehension = [ kelement for kelement in LST_Initial ] :\n{ LST_Comprehension } \n" )
print ( f"LST_Superficiel = copy.copy ( LST_Initial ) :\n{ LST_Superficiel } \n" )
print ( f"LST_Profond = copy.deepcopy ( LST_Initial ) :\n{ LST_Profond }" )
retourne :
Liste initiale : [1, 2, 3, 'A', 'B', 'C', [11, 22, 33, 'abc'], True, False, None]
Liste modifiée : [1, 2, 'modifié', 'A', 'B', 'inséré', 'C', [11, 22, 'trente trois', 'abc'], True, False, 'ajouté']
Valeur des différentes copies de la liste initiale :
LST_Affectation = LST_Initial:
[1, 2, 'modifié', 'A', 'B', 'inséré', 'C', [11, 22, 'trente trois', 'abc'], True, False, 'ajouté']
LST_Tranche = LST_Initial [ : ] :
[1, 2, 3, 'A', 'B', 'C', [11, 22, 'trente trois', 'abc'], True, False, None]
LST_Copie = LST_Initial.copy ( ) :
[1, 2, 3, 'A', 'B', 'C', [11, 22, 'trente trois', 'abc'], True, False, None]
LST_Comprehension = [ kelement for kelement in LST_Initial ] :
[1, 2, 3, 'A', 'B', 'C', [11, 22, 'trente trois', 'abc'], True, False, None]
LST_Superficiel = copy.copy ( LST_Initial ) :
[1, 2, 3, 'A', 'B', 'C', [11, 22, 'trente trois', 'abc'], True, False, None]
LST_Profond = copy.deepcopy ( LST_Initial ) :
[1, 2, 3, 'A', 'B', 'C', [11, 22, 33, 'abc'], True, False, None]
Votre aide est précieuse pour améliorer ce site, alors n'hésitez pas à faire part de
Dans la LOGITHEQUE de MON PYTHON PAS A PAS
vous trouvez des suggestions de projets simples et classiques pour
ooo
TESTER - DÉCOUVRIR - PRATIQUER - APPROFONDIR - EXPLORER
ooo
la programmation récréative avec le langage PYTHON 3
avec un exemple de résolution à télécharger pour vous inspirer.