+ 5
needed help with a code why does changing b changes a as well
a=[2,2,2,2,2,2,2] k=2 b=a count=0 for i in range(1,len(a)): b[i]=b[i-1]+k for i in range(len(a)): if a[i]==b[i]: pass else: count+1 print(a,b)
5 Réponses
+ 3
Becase "b" its a reference of "a" content (a list)... You have to copy the list like:
b= a[:]
+ 11
good
+ 3
a = [1, 2, 3]
b = a
b == a # True
b is a # True
In the above, b and a point to the same places in memory. Changing one will change the other because they are the same.
c = a[:]
c == a # True
c is a # False
In the above c has the same values as a, but they’re saved in a different place in memory. Then you can change one list without changing the other.
+ 3
thanks guys