Forum » Programiranje » [Python] Odkritje današnjega dne: deepcopy
[Python] Odkritje današnjega dne: deepcopy

HotBurek ::
Pozdravljeni.
Evo, da pošeram, kaj sem danes odkril.
Imel sem loop, v katerem se kopiral "osnovni" objekt, mu dodal neko lastnost, in potem append-al v list.
Primer:
Output:
Porabil sem eno uro, da sem ugotovil, kako je mogoče, da zadnji append "povozi" vse prejšne. Razlog bi naj bil v tem, da ker kopira referenco na objek in ne naredi kopije.
Ena izmed možnih rešitev:
Output:
To pa štima.
Ne vem, zakaj tega prej nisem opazil, da kakšna stvar ne bi delala pravilno.
No, sedaj je popravljeno.
Well done.
Evo, da pošeram, kaj sem danes odkril.
Imel sem loop, v katerem se kopiral "osnovni" objekt, mu dodal neko lastnost, in potem append-al v list.
Primer:
item1 = { "name": "test" }; list1 = []; for i in range(0 ,3): itemx = item1; itemx["count"] = str(i); list1.append(itemx); print(list1);
Output:
[{'name': 'test', 'count': '2'}, {'name': 'test', 'count': '2'}, {'name': 'test', 'count': '2'}]
Porabil sem eno uro, da sem ugotovil, kako je mogoče, da zadnji append "povozi" vse prejšne. Razlog bi naj bil v tem, da ker kopira referenco na objek in ne naredi kopije.
Ena izmed možnih rešitev:
import copy; item1 = { "name": "test" }; list1 = []; for i in range(0 ,3): itemx = item1; itemx["count"] = str(i); list2.append(copy.deepcopy(itemx)); print(list1);
Output:
[{'name': 'test', 'count': '0'}, {'name': 'test', 'count': '1'}, {'name': 'test', 'count': '2'}]
To pa štima.
Ne vem, zakaj tega prej nisem opazil, da kakšna stvar ne bi delala pravilno.
No, sedaj je popravljeno.
Well done.
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
- spremenilo: HotBurek ()

sbawe64 ::
In Python, copy refers to creating a shallow copy of an object, whereas copy.deepcopy creates a deep copy of an object. Here's how they differ:
Shallow Copy (copy): Creates a new object and inserts references into it to the objects found in the original. Changes to the original object will affect the shallow copy if the referenced objects are mutable. For instance, if you modify a list within a list, the change will be reflected in both the original list and the shallow copy
Deep Copy (copy.deepcopy): Creates a new object and recursively adds copies of the objects found in the original. This means that the deep copy does not share any references with the original object. Therefore, changes to the original object will not affect the deep copy, even if the objects are mutable
na primeru pojasnjeno
https://www.geeksforgeeks.org/copy-pyth...
Shallow Copy (copy): Creates a new object and inserts references into it to the objects found in the original. Changes to the original object will affect the shallow copy if the referenced objects are mutable. For instance, if you modify a list within a list, the change will be reflected in both the original list and the shallow copy
Deep Copy (copy.deepcopy): Creates a new object and recursively adds copies of the objects found in the original. This means that the deep copy does not share any references with the original object. Therefore, changes to the original object will not affect the deep copy, even if the objects are mutable
na primeru pojasnjeno
https://www.geeksforgeeks.org/copy-pyth...
2020 is new 1984
Corona World order
Corona World order
Zgodovina sprememb…
- spremenilo: sbawe64 ()

Rias Gremory ::
@sbawe64
Well done.
Well done.
Mirno gledamo, kako naš svet propada,
saj za časa našega življenja ne bo popolnoma propadel.
saj za časa našega življenja ne bo popolnoma propadel.
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | vba arrayOddelek: Programiranje | 949 (642) | Vazelin |
» | [C#] WPF vprašanjaOddelek: Programiranje | 1149 (771) | Ciklamen |
» | Coursera naloga (python)Oddelek: Programiranje | 2030 (1658) | jype |
» | batch fajl iz txt (strani: 1 2 )Oddelek: Programiranje | 9940 (8601) | b3D_950 |
» | Prireditev class = classOddelek: Programiranje | 1890 (1555) | noraguta |