Bonjour,
j'ai une question par rapport au fonctionnement de python. À un moment dans mon code, j'écris: D1 = D1 + 1, où D1 est une matrice. Je veux vraiment faire une addition element-wise, et non agrandir ma matrice. Un jour, pour rendre mon code plus beau, j'ai pensé écrire D1 += 1. Et alors plus rien ne fonctionnait. J'ai voulu chercher pourquoi sur google, mais je ne suis pas sûre de comprendre la réponse. Le forum suivant semble assez bon:
http://stackoverflow.com/questions/2347265/what-does-plus-equals-do-in-python. Mais dans leur cas, ils veulent vraiment augmenter la liste. Moi, non, alors je ne comprends pas pourquoi mon code bogue.
Un indice, peut-être. D1 est copié dans autre matrice, D2. Pourtant, j'ai bien fait attention de faire D2 = D1.copy(). Voici mon code, de façon plus complète:
D2 = D1.copy()
D1 = D1 + 1
while(D n'a pas convergé, donc tant que la différence entre D1 et D2 est grande):
D1 = D2.copy()
D2 = ...
fin du while
Donc mon +1 ne servait qu'à rendre D1 différent de D2 pour pouvoir entrer dans la boucle la première fois. Ce n'est probablement pas la plus belle technique, mais maintenant ça m'a rendu curieuse, pour le +=.
--------------------------------------------------------
Hi,
I have a question concerning python. In my code, somewhere, I write D1 = D1 + 1, where D1 is a matrix. I do really want to add 1 element-wise, not to add it add the end. Then, I thought I could have a code more beautiful if I wrote D1 += 1 instead. But then nothing would work anymore. I looked for the reason on google, but I'm not so sure I understand the answer. This forum seems pretty good:
http://stackoverflow.com/questions/2347265/what-does-plus-equals-do-in-python. But they want to add their item at the end of the list, which is not exactly my problem. So I still don't understand what's the bug in my code.
Maybe I have a clue, though. D1 is copied in another matrix, D2. But I thought I had done it wright; I really wrote D2 = D1.copy(). I copy here my code, maybe it's easyer to see.
D2 = D1.copy()
D1 = D1 + 1
while(D hasn't converged, so while the difference between D1 and D2 is big):
D1 = D2.copy()
D2 = ...
end of while
So the reason for my +1 was to be sure D1 would be different of D2 the first time, to enter the while loop. It might not be the best way, but now it made me curious about the +=.
Thank you!
Emmanuelle