I am trying to create a new variable that will consist of an existing dictionary so that I can change things in this new dictionary without affecting the old one. When I try this below, which I think will be the obvious way to do this, it still seems to be editing my original dictionary when I make changes for the new one. I searched for information about this, but can't seem to find anything, any information is appreciated
newdictionary = olddictionary
You create a link instead of a copy. To make a full copy and leave the original untouched, you need copy.deepcopy(). So:
copy.deepcopy()
from copy import deepcopy dictionary_new = deepcopy(dictionary_old)
a = dict(b) a = b.copy() ( , , ).
a = dict(b)
a = b.copy()
, newdictionary olddictionary. . ( , dicts).
newdictionary
olddictionary
.copy() ( : ):
.copy()
newdictionary = olddictionary.copy()
, .deepcopy()
.deepcopy()
newdictionary = copy.deepcopy(olddictionary)
Shallow vs Deep Copy
, Python, , newdictionary , olddictionary, . dict():
dict()
newdictionary = dict(olddictionary)
, . . copy.
copy
newdictionary = dict(olddictionary.items())
( , olddict (, ) dict, ( , )).
: , - , .
a = b
, .
.
: ( , ):
new = dict(old) new = old.copy() import copy new = copy.copy(old) import copy new = copy.deepcopy(old)
, , . . .
, dict.copy() , .
from copy import deepcopy d = {} d['names'] = ['Alfred', 'Bertrand'] c = d.copy() dc = deepcopy(d)
Source: https://habr.com/ru/post/1760624/More articles:Is there an IntelliJ plugin for publishing code snippets in Gist - javaConvert string "0x32" to one byte - c #Change WizardSmallBitmapImage in Inno Setup Uninstaller - windowsInsertion into the table selects the result set from the stored procedure, but the number of columns is not the same - sqlRhino mocks .Repeat.Any () doesn't work for me - c #Force Resharper open constructor when navigating with CTRL + SHIFT + T - visual-studioTeX: add a blank page after each content page - latexCan I create a web service that transfers a PDF file for download - c #How to replace CHAR (13) in DB varchar (6000)? - coldfusionHow to set value for checkbox via EmbeddedWB.FillForm? (Delphi) - delphiAll Articles