A bug of Python is getting my crayzy please help

A bug of Python is getting my crayzy please help

Postby Abraxas » Fri Apr 08, 2016 2:42 am

I'm trying to copy a list (old) into a new list (new), but when a make any change in new the same happens to old. I'm using [:], copy.deepcopy() and even I make old a tuple and then new = list(old). But the problem goes on as the same. Please I need to know why and hoe avoid it.

Image
Last edited by micseydel on Fri Apr 08, 2016 2:58 am, edited 1 time in total.
Reason: Initial post lock.
Abraxas
 
Posts: 5
Joined: Fri Apr 08, 2016 2:34 am

Re: A bug of Python is getting my crayzy please help

Postby Ofnuts » Fri Apr 08, 2016 8:49 am

Your first test checks that pizzara and pizzara2 are two different lists. But their elements can refer to identical lists... Check that pizzara[0] and pizzara2[0] are indeed different lists themselves.
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Re: A bug of Python is getting my crayzy please help

Postby Abraxas » Sun Apr 10, 2016 6:18 pm

":o"

You're so right!!!!!

The inner elements of that two different lists are the same!!!

Let me see how can I deal with that!

Thank for your reply.
Abraxas
 
Posts: 5
Joined: Fri Apr 08, 2016 2:34 am

Re: A bug of Python is getting my crayzy please help

Postby Ofnuts » Sun Apr 10, 2016 7:53 pm

Btw, even though Python is likely not bug-free, when something doesn't work as you expect, the chances are overwhelmingly in favor of a problem in your code, v.s. a bug in Python.
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Re: A bug of Python is getting my crayzy please help

Postby Abraxas » Mon Apr 11, 2016 8:15 am

Nothing... I don't know how to deal with my problem.

Code: Select all
import copy

old =  [['111',[1,2,3,4,5,6,7,8,9]],['112',[1,2,3,4,5,6,7,8,9]],['113',[1,2,3,4,5,6,7,8,9]],
            ['214',[1,2,3,4,5,6,7,8,9]],['215',[1,2,3,4,5,6,7,8,9]],['216',[1,2,3,4,5,6,7,8,9]],
            ['317',[1,2,3,4,5,6,7,8,9]],['318',[1,2,3,4,5,6,7,8,9]],['319',[1,2,3,4,5,6,7,8,9]],
            ['121',[1,2,3,4,5,6,7,8,9]],['122',[1,2,3,4,5,6,7,8,9]],['123',[1,2,3,4,5,6,7,8,9]],
            ['224',[1,2,3,4,5,6,7,8,9]],['225',[1,2,3,4,5,6,7,8,9]],['226',[1,2,3,4,5,6,7,8,9]],
            ['327',[1,2,3,4,5,6,7,8,9]],['328',[1,2,3,4,5,6,7,8,9]],['329',[1,2,3,4,5,6,7,8,9]],
            ['131',[1,2,3,4,5,6,7,8,9]],['132',[1,2,3,4,5,6,7,8,9]],['133',[1,2,3,4,5,6,7,8,9]],
            ['234',[1,2,3,4,5,6,7,8,9]],['235',[1,2,3,4,5,6,7,8,9]],['236',[1,2,3,4,5,6,7,8,9]],
            ['337',[1,2,3,4,5,6,7,8,9]],['338',[1,2,3,4,5,6,7,8,9]],['339',[1,2,3,4,5,6,7,8,9]],
            ['441',[1,2,3,4,5,6,7,8,9]],['442',[1,2,3,4,5,6,7,8,9]],['443',[1,2,3,4,5,6,7,8,9]],
            ['544',[1,2,3,4,5,6,7,8,9]],['545',[1,2,3,4,5,6,7,8,9]],['546',[1,2,3,4,5,6,7,8,9]],
            ['647',[1,2,3,4,5,6,7,8,9]],['648',[1,2,3,4,5,6,7,8,9]],['649',[1,2,3,4,5,6,7,8,9]],
            ['451',[1,2,3,4,5,6,7,8,9]],['452',[1,2,3,4,5,6,7,8,9]],['453',[1,2,3,4,5,6,7,8,9]],
            ['554',[1,2,3,4,5,6,7,8,9]],['555',[1,2,3,4,5,6,7,8,9]],['556',[1,2,3,4,5,6,7,8,9]],
            ['657',[1,2,3,4,5,6,7,8,9]],['658',[1,2,3,4,5,6,7,8,9]],['659',[1,2,3,4,5,6,7,8,9]],
            ['461',[1,2,3,4,5,6,7,8,9]],['462',[1,2,3,4,5,6,7,8,9]],['463',[1,2,3,4,5,6,7,8,9]],
            ['564',[1,2,3,4,5,6,7,8,9]],['565',[1,2,3,4,5,6,7,8,9]],['566',[1,2,3,4,5,6,7,8,9]],
            ['667',[1,2,3,4,5,6,7,8,9]],['668',[1,2,3,4,5,6,7,8,9]],['669',[1,2,3,4,5,6,7,8,9]],
            ['771',[1,2,3,4,5,6,7,8,9]],['772',[1,2,3,4,5,6,7,8,9]],['773',[1,2,3,4,5,6,7,8,9]],
            ['874',[1,2,3,4,5,6,7,8,9]],['875',[1,2,3,4,5,6,7,8,9]],['876',[1,2,3,4,5,6,7,8,9]],
            ['977',[1,2,3,4,5,6,7,8,9]],['978',[1,2,3,4,5,6,7,8,9]],['979',[1,2,3,4,5,6,7,8,9]],
            ['781',[1,2,3,4,5,6,7,8,9]],['782',[1,2,3,4,5,6,7,8,9]],['783',[1,2,3,4,5,6,7,8,9]],
            ['884',[1,2,3,4,5,6,7,8,9]],['885',[1,2,3,4,5,6,7,8,9]],['886',[1,2,3,4,5,6,7,8,9]],
            ['987',[1,2,3,4,5,6,7,8,9]],['988',[1,2,3,4,5,6,7,8,9]],['989',[1,2,3,4,5,6,7,8,9]],
            ['791',[1,2,3,4,5,6,7,8,9]],['792',[1,2,3,4,5,6,7,8,9]],['793',[1,2,3,4,5,6,7,8,9]],
            ['894',[1,2,3,4,5,6,7,8,9]],['895',[1,2,3,4,5,6,7,8,9]],['896',[1,2,3,4,5,6,7,8,9]],
            ['997',[1,2,3,4,5,6,7,8,9]],['998',[1,2,3,4,5,6,7,8,9]],['999',[1,2,3,4,5,6,7,8,9]]]

def cloning(p):
    global new
    new = list()
    for i in p:
        new.append(list())
    for (inj,j) in enumerate(p):
        new[inj].append(copy.deepcopy(j[0][:]))
        new[inj].append(copy.deepcopy(j[1][:]))
    return new

new = None

cloning(old)


I need to iterate over "new" several times without changing "old"

I'm trying the code above, but with poor results because the inner elements of the lists are always the same.

Could someone give any suggestion or advice?

Thanks a lot.
Last edited by Abraxas on Mon Apr 11, 2016 8:30 am, edited 1 time in total.
Abraxas
 
Posts: 5
Joined: Fri Apr 08, 2016 2:34 am

Re: A bug of Python is getting my crayzy please help

Postby Abraxas » Mon Apr 11, 2016 8:23 am

Ofnuts wrote:Btw, even though Python is likely not bug-free, when something doesn't work as you expect, the chances are overwhelmingly in favor of a problem in your code, v.s. a bug in Python.


Je suis desolé. That is true it isn't a bug of python, I wished to change the title of this thread but I can't. Sorry again.
Abraxas
 
Posts: 5
Joined: Fri Apr 08, 2016 2:34 am

Re: A bug of Python is getting my crayzy please help

Postby Ofnuts » Mon Apr 11, 2016 11:21 am

You're making it more complicated than necessary. Why don't you deep-copy the top elements:
Code: Select all
import copy

old = [['111',[1,2,3,4]],['112',[1,2,3,4]],['113',[1,2,3,4]],
       ['214',[1,2,3,4]],['215',[1,2,3,4]],['216',[1,2,3,4]],
       ['317',[1,2,3,4]],['318',[1,2,3,4]],['319',[1,2,3,4]],
       ['121',[1,2,3,4]],['122',[1,2,3,4]],['123',[1,2,3,4]]]

new=copy.deepcopy(old)

print old[0][1]
print new[0][1] is old[0][1] # check that we have distinct objects
print '--------'
print old[0][1][2]
new[0][1][2]=1000 # update element in 'new'
print new[0][1][2]
print old[0][1][2] # element in 'old' is not changed.

yields:
Code: Select all
[1, 2, 3, 4]
False
--------
3
1000
3
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Re: A bug of Python is getting my crayzy please help

Postby Abraxas » Mon Apr 11, 2016 1:15 pm

Ofnuts wrote:You're making it more complicated than necessary. Why don't you deep-copy the top elements:
Code: Select all
import copy

old = [['111',[1,2,3,4]],['112',[1,2,3,4]],['113',[1,2,3,4]],
       ['214',[1,2,3,4]],['215',[1,2,3,4]],['216',[1,2,3,4]],
       ['317',[1,2,3,4]],['318',[1,2,3,4]],['319',[1,2,3,4]],
       ['121',[1,2,3,4]],['122',[1,2,3,4]],['123',[1,2,3,4]]]

new=copy.deepcopy(old)

print old[0][1]
print new[0][1] is old[0][1] # check that we have distinct objects
print '--------'
print old[0][1][2]
new[0][1][2]=1000 # update element in 'new'
print new[0][1][2]
print old[0][1][2] # element in 'old' is not changed.

yields:
Code: Select all
[1, 2, 3, 4]
False
--------
3
1000
3


Wow! Such stupid I'm!!!

Thanks a lot!!!!

You save me from my own stupidity!!! :)
Abraxas
 
Posts: 5
Joined: Fri Apr 08, 2016 2:34 am


Return to Bar

Who is online

Users browsing this forum: No registered users and 1 guest