Posts: 15
Threads: 4
Joined: Apr 2019
May-28-2019, 08:30 PM
(This post was last modified: May-28-2019, 08:33 PM by ivinjjunior.)
so if i put some very different name it might work?
actually i remember now, i have already done it, i'v named the "pedidosafe" as "hahaha" ( i was frustrated) and the problem still persisted
Posts: 2,168
Threads: 35
Joined: Sep 2016
May-28-2019, 08:43 PM
(This post was last modified: May-28-2019, 08:46 PM by Yoriz.)
No changing the variable name wont do anything , it still points at the same objects
you need to use
https://docs.python.org/3/library/copy.h...y.deepcopy Wrote:The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import copy
innerlist1 = [ 11111 , 22222 , 33333 , 44444 , 55555 ]
innerlist2 = [ 0 , 77777 , 88888 , 22222 ]
innerlist3 = [ 22222 , 11111 , 33333 , 44444 ]
innerlist4 = [ 11111 , 0 ], [ 55555 , 88888 ]
innerlist5 = [ 77777 , 99999 , 11111 ]
innerlist6 = [ 22222 , 55555 , 33333 , 99999 ]
innerlist7 = [ 55555 , 22222 , 11111 , 33333 , 0 , 44444 , 77777 ]
outerlist = [
innerlist1,
innerlist2,
innerlist3,
innerlist4,
innerlist5,
innerlist6,
innerlist7,
]
outerlistcopy = copy.deepcopy(outerlist)
print (outerlistcopy)
innerlist1[ 0 ] = 99999
print (outerlistcopy)
print (outerlist)
|
Output: [[11111, 22222, 33333, 44444, 55555], [0, 77777, 88888, 22222], [22222, 11111, 33333, 44444], ([11111, 0], [55555, 88888]), [77777, 99999, 11111], [22222, 55555, 33333, 99999], [55555, 22222, 11111, 33333, 0, 44444, 77777]]
[[11111, 22222, 33333, 44444, 55555], [0, 77777, 88888, 22222], [22222, 11111, 33333, 44444], ([11111, 0], [55555, 88888]), [77777, 99999, 11111], [22222, 55555, 33333, 99999], [55555, 22222, 11111, 33333, 0, 44444, 77777]]
[[99999, 22222, 33333, 44444, 55555], [0, 77777, 88888, 22222], [22222, 11111, 33333, 44444], ([11111, 0], [55555, 88888]), [77777, 99999, 11111], [22222, 55555, 33333, 99999], [55555, 22222, 11111, 33333, 0, 44444, 77777]]
Posts: 15
Threads: 4
Joined: Apr 2019
I am very beginner but i will try it out, if i got it right i need a deep copy of the list "pedidosafe" right?
like:
deepcopy(pedidosafe.append(itens_pedido[:]))
?
or pedidosafe.deepcopy()
i don know how to use it
Posts: 2,168
Threads: 35
Joined: Sep 2016
add the import copy
change
1 |
pedidosafe.append(itens_pedido[:])
|
to
1 |
pedidosafe.append(copy.deepcopy(itens_pedido))
|
Posts: 15
Threads: 4
Joined: Apr 2019
oh my good!! i got iiiit!! finally
i used this copy.copy in the end of the code.. it was the end of it that was creating the link between the lists..
Output: if menu != 0:
controle_posi = 0
pedidos.clear()
pedidosbk.clear()
itendupl.clear()
juntar.clear()
for restart in pedidosafe:
pedidos.append(restart)
pedidosbk.append(restart)
menu = 0
break
i replaced for :
Output: if menu != 0:
controle_posi = 0
pedidos.clear()
pedidosbk.clear()
itendupl.clear()
juntar.clear()
for restart in pedidosafe:
pedidos.append(copy.copy(restart))
pedidosbk.append(copy.copy(restart))
menu = 0
break
Posts: 15
Threads: 4
Joined: Apr 2019
Working fine! Thank you :)
|