[python] append lijkt te vervangen

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

  • n3ck
  • Registratie: Mei 2002
  • Laatst online: 24-07 19:47
Ik wilde een stukje code schrijven om elke keer een resultaat weg te schrijven naar een Array. Ik zag echter wat raars gebeuren. Onderstaande
Python:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
j=0
arraywithallresults=[]
for i in range(0,3):
    result=[]
    result.append(i)
    print i,j,'result', result
    arraywithallresults.append(result)
    print i,j,'arraywithallresults', arraywithallresults
    for j in range(i+1,3):
        result.append(j)
        print i,j,'result', result
        arraywithallresults.append(result)
        print i,j,'arraywithallresults', arraywithallresults

print arraywithallresults

resulteert in:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
0 0 result [0]
0 0 arraywithallresults [[0]]
0 1 result [0, 1]
0 1 arraywithallresults [[0, 1], [0, 1]]
0 2 result [0, 1, 2]
0 2 arraywithallresults [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
1 2 result [1]
1 2 arraywithallresults [[0, 1, 2], [0, 1, 2], [0, 1, 2], [1]]
1 2 result [1, 2]
1 2 arraywithallresults [[0, 1, 2], [0, 1, 2], [0, 1, 2], [1, 2], [1, 2]]
2 2 result [2]
2 2 arraywithallresults [[0, 1, 2], [0, 1, 2], [0, 1, 2], [1, 2], [1, 2], [2]]
[[0, 1, 2], [0, 1, 2], [0, 1, 2], [1, 2], [1, 2], [2]]


ik zou verwachten dat ik uiteindelijk alle waarden uit de variabele result in arraywithallresults zou krijgen.. wat er echter gebeurt is dat het lijkt alsof alle items op een rare manier vervangen worden. Het gekste van dit verhaal vind ik dat als ik deze
Python:
1
result.append(j)
regel uitsluit in de tweede for loop:
Python:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
j=0
arraywithallresults=[]
for i in range(0,3):
    result=[]
    result.append(i)
    print i,j,'result', result
    arraywithallresults.append(result)
    print i,j,'arraywithallresults', arraywithallresults
    for j in range(i+1,3):
        #result.append(j)
        print i,j,'result', result
        arraywithallresults.append(result)
        print i,j,'arraywithallresults', arraywithallresults

print arraywithallresults


ik de volgende uitkomst krijg:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
0 0 result [0]
0 0 arraywithallresults [[0]]
0 1 result [0]
0 1 arraywithallresults [[0], [0]]
0 2 result [0]
0 2 arraywithallresults [[0], [0], [0]]
1 2 result [1]
1 2 arraywithallresults [[0], [0], [0], [1]]
1 2 result [1]
1 2 arraywithallresults [[0], [0], [0], [1], [1]]
2 2 result [2]
2 2 arraywithallresults [[0], [0], [0], [1], [1], [2]]
[[0], [0], [0], [1], [1], [2]]


wat iets meer in mijn lijn van de verwachting ligt. Ben ik nou helemaal scheel aan het kijken vandaag of is dit inderdaad apart?

edit:
En schiet u mij maar helemaal lek:
Python:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
j=0
s=''
arraywithallresults=[]
for i in range(0,3):
    result=[]
    result.append(i)
    print i,j,'result', result
    arraywithallresults.append(result)
    print i,j,'arraywithallresults', arraywithallresults
    s+=str(result)
    for j in range(i+1,3):
        result.append(j)
        print i,j,'result', result
        arraywithallresults.append(result)
        print i,j,'arraywithallresults', arraywithallresults
        s+=str(result)

print arraywithallresults

print s


geeft:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
0 0 result [0]
0 0 arraywithallresults [[0]]
0 1 result [0, 1]
0 1 arraywithallresults [[0, 1], [0, 1]]
0 2 result [0, 1, 2]
0 2 arraywithallresults [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
1 2 result [1]
1 2 arraywithallresults [[0, 1, 2], [0, 1, 2], [0, 1, 2], [1]]
1 2 result [1, 2]
1 2 arraywithallresults [[0, 1, 2], [0, 1, 2], [0, 1, 2], [1, 2], [1, 2]]
2 2 result [2]
2 2 arraywithallresults [[0, 1, 2], [0, 1, 2], [0, 1, 2], [1, 2], [1, 2], [2]]
[[0, 1, 2], [0, 1, 2], [0, 1, 2], [1, 2], [1, 2], [2]]
[0][0, 1][0, 1, 2][1][1, 2][2]

Dus: op het moment dat ik de array eerst naar een string converteer gaat het wel goed 8)7 Dat wordt dus om dit interessante fenomeen heen programmeren met een split zodat ik in het beste programmeervoorbeelden topic terug mag komen 8)

[ Voor 21% gewijzigd door n3ck op 13-02-2015 18:06 ]


Acties:
  • 0 Henk 'm!

  • Nvidiot
  • Registratie: Mei 2003
  • Laatst online: 03-06 16:38

Nvidiot

notepad!

Python:
1
2
3
4
5
x = [4, 2]
y = []
y.append(x)
id(x)
id(y[0])

id() geeft je aan waar in het geheugen het object staat. Als je dit uitvoert zie je dat x en y[0] hetzelfde object zijn. Als je dus na de y.append(x) het object x aanpast, zal y[0] ook aanpassen!

Oplossing: 'copy.copy()' gebruiken, of:
code:
1
y.append(x[:])

What a caterpillar calls the end, the rest of the world calls a butterfly. (Lao-Tze)


Acties:
  • 0 Henk 'm!

  • n3ck
  • Registratie: Mei 2002
  • Laatst online: 24-07 19:47
Dit verklaart inderdaad een en ander. Ik had al het gevoel dat beide arrays magischerwijs met elkaar verbonden waren.. maar ik dacht eigenlijk dat dat niet zou kunnen. Thanks in ieder geval!!