Ik wilde een stukje code schrijven om elke keer een resultaat weg te schrijven naar een Array. Ik zag echter wat raars gebeuren. Onderstaande
resulteert in:
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
ik de volgende uitkomst krijg:
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:
geeft:
Dus: op het moment dat ik de array eerst naar een string converteer gaat het wel goed
Dat wordt dus om dit interessante fenomeen heen programmeren met een split zodat ik in het beste programmeervoorbeelden topic terug mag komen
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:
regel uitsluit in de tweede for loop:1
| result.append(j) |
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

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