Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
my list is being changed
#11
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
Reply
#12
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.

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]]
Reply
#13
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
Reply
#14
add the import copy
change
pedidosafe.append(itens_pedido[:])
to
pedidosafe.append(copy.deepcopy(itens_pedido))
Reply
#15
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
Reply
#16
Working fine! Thank you :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The behavior of tune model has changed Led_Zeppelin 5 4,358 Oct-21-2021, 06:52 PM
Last Post: jefsummers
  how can a variable change if I haven't changed it? niminim 5 2,992 Apr-07-2021, 06:57 PM
Last Post: niminim
  RuntimeError: dictionary changed size during iteration Shreeniket987 3 3,678 Jun-01-2019, 01:22 PM
Last Post: buran
  RuntimeError: dictionary changed size during iteration anna 4 3,439 Feb-20-2019, 11:04 AM
Last Post: anna
  how to work with variables changed in definitions Prof_Jar_Jar 2 2,578 Dec-16-2018, 12:04 AM
Last Post: Prof_Jar_Jar
  RuntimeError: dictionary changed size during iteration Skaperen 1 9,104 Dec-10-2018, 10:14 PM
Last Post: nilamo
  Saving Values Changed in a database themick789 1 2,249 Nov-28-2018, 08:16 AM
Last Post: Larz60+
  Strange the name of the file is changed automaticaly sylas 3 3,159 Jun-05-2018, 06:43 PM
Last Post: buran
  How can I use GNU readline when the value of sys.stdin has been changed? thePhysicist8 6 7,002 Dec-30-2016, 10:09 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020