+ 1

why the answer is wrong?!

why in this code, 't' will be changed? S = [[511 for i in range(3)] for j in range(3)] t = S.copy() S[0][2] = 76 print(t)

27th Apr 2019, 10:25 AM
رضا شفقی
رضا شفقی - avatar
4 Respostas
+ 6
Because t is just another label that is attached to the same list. Changing S changes t as well. To create an independent list, use deepcopy: from copy import deepcopy S = [[511 for i in range(3)] for j in range(3)] t = deepcopy(S)
27th Apr 2019, 10:43 AM
Anna
Anna - avatar
+ 3
What answer are you reffering to? Please add more informations to your question.
27th Apr 2019, 10:28 AM
Dragonxiv
Dragonxiv - avatar
+ 1
@Anna tell me more about 'list.copy()' and its properties.
28th Apr 2019, 6:17 AM
رضا شفقی
رضا شفقی - avatar
+ 1
@Dragonxiv, I want a backup of my list, but it'll be changed.
28th Apr 2019, 6:19 AM
رضا شفقی
رضا شفقی - avatar