deep-copy

copying 2D arrays in python

copying 2D arrays in python Question: I’m trying to copy a 2D array to another 2D array in python, then change the copy, without changing any other 2D arrays which are a copy of the original 2D array and they’re acting rather strangely. Upon consulting stackoverflow when I first needed to copy 2D arrays into …

Total answers: 2

Why it seems contradictory when yield meet recursion in python?

Why it seems contradictory when yield meet recursion in python? Question: I tried to implement full permutation via python generator & recursion, but the output from ‘print’ is different from any other form of generator utility. def gen_perms(seq): seq_len = len(seq) def swap(i,j): x = seq[i] seq[i] = seq[j] seq[j] = x def perms(n): if …

Total answers: 1

Python dataclass: Forcing a dictionary field to be a deep copy

Python dataclass: Forcing a dictionary field to be a deep copy Question: I am working with a dataclass that contains a dict. I want the dict to be a deepcopy, without having to rely on a post_init call, which would basically void to interest of a dataclass What would be a nice solution ? from …

Total answers: 1

Is there a way to deepcopy a list without importing copy.deepcopy?

Is there a way to deepcopy a list without importing copy.deepcopy? Question: x = [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] b = x[:] b[1].append(99) print(x[1]) x[1]=[1, 2, 3, 99] Using the builtin .copy() method b=x.copy() is the same, x[1]=[1, 2, 3, 99] and b = list(x) is the same …

Total answers: 1

How to copy a Python class instance if deepcopy() does not work?

How to copy a Python class instance if deepcopy() does not work? Question: I would like to make a copy of a class instance in python. I tried copy.deepcopy but I get the error message: RuntimeError: Only Variables created explicitly by the user (graph leaves) support the deepcopy protocol at the moment So suppose I …

Total answers: 3

python Pandas DataFrame copy(deep=False) vs copy(deep=True) vs '='

python Pandas DataFrame copy(deep=False) vs copy(deep=True) vs '=' Question: Could somebody explain to me a difference between df2 = df1 df2 = df1.copy() df3 = df1.copy(deep=False) I have tried all options and did as follows: df1 = pd.DataFrame([1,2,3,4,5]) df2 = df1 df3 = df1.copy() df4 = df1.copy(deep=False) df1 = pd.DataFrame([9,9,9]) and returned as follows: df1: …

Total answers: 3

How to create an independent copy of a function in python?

How to create an independent copy of a function in python? Question: Is it possible in python to create an un-linked copy of a function? For example, if I have a = lambda(x): x b = lambda(x): a(x)+1 I want b(x) to always return x+1, regardless if a(x) is modified not. Currently, if I do …

Total answers: 2

Relationship between pickle and deepcopy

Relationship between pickle and deepcopy Question: What exactly is the relationship between pickle and copy.deepcopy? What mechanisms do they share, and how? It is clear the two are closely-related operations, and share some of the mechanisms/protocols, but I can’t wrap my head around the details. Some (confusing) things I found out: If a class defines …

Total answers: 2

Python: deepcopy does not work on user-defined classes?

Python: deepcopy does not work on user-defined classes? Question: In the following example I would expect deepcopy to create a copy of field and not just copy the reference. What happens here and is there an easy way around it? from copy import deepcopy class Test: field = [(1,2)] t1 = Test() t2 = deepcopy(t1) …

Total answers: 2

How to deep copy a list?

How to deep copy a list? Question: After E0_copy = list(E0), I guess E0_copy is a deep copy of E0 since id(E0) is not equal to id(E0_copy). Then I modify E0_copy in the loop, but why is E0 not the same after? E0 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] for k …

Total answers: 10