» »

[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:
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
  • spremenilo: HotBurek ()

HolyFuck1 ::

Well done.


Well done z novo troll temo.

FireSnake ::

Well done?
To sam sebi čestitaš za kaj že?
Poglej in se nasmej: vicmaher.si

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...
2020 is new 1984
Corona World order

Zgodovina sprememb…

  • spremenilo: sbawe64 ()

Rias Gremory ::

@sbawe64
Well done.
Mirno gledamo, kako naš svet propada,
saj za časa našega življenja ne bo popolnoma propadel.


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

vba array

Oddelek: Programiranje
12871 (564) Vazelin
»

[C#] WPF vprašanja

Oddelek: Programiranje
101074 (696) Ciklamen
»

Coursera naloga (python)

Oddelek: Programiranje
161817 (1445) jype
»

batch fajl iz txt (strani: 1 2 )

Oddelek: Programiranje
658955 (7616) b3D_950
»

Prireditev class = class

Oddelek: Programiranje
131770 (1435) noraguta

Več podobnih tem